wintest: make IPv6 optional in wintest
[samba.git] / wintest / test-s4-howto.py
1 #!/usr/bin/env python
2
3 '''automated testing of the steps of the Samba4 HOWTO'''
4
5 import sys, os
6 import optparse
7 import wintest, pexpect
8
9 def check_prerequesites(t):
10     t.info("Checking prerequesites")
11     t.setvar('HOSTNAME', t.cmd_output("hostname -s").strip())
12     if os.getuid() != 0:
13         raise Exception("You must run this script as root")
14     t.putenv("KRB5_CONFIG", '${PREFIX}/private/krb5.conf')
15     t.run_cmd('ifconfig ${INTERFACE} ${INTERFACE_NET} up')
16     if t.getvar('INTERFACE_IPV6'):
17         t.run_cmd('ifconfig ${INTERFACE} inet6 del ${INTERFACE_IPV6}/64', checkfail=False)
18         t.run_cmd('ifconfig ${INTERFACE} inet6 add ${INTERFACE_IPV6}/64 up')
19
20
21 def build_s4(t):
22     '''build samba4'''
23     t.info('Building s4')
24     t.chdir('${SOURCETREE}/source4')
25     t.putenv('CC', 'ccache gcc')
26     t.run_cmd('make reconfigure || ./configure --enable-auto-reconfigure --enable-developer --prefix=${PREFIX} -C')
27     t.run_cmd('make -j')
28     t.run_cmd('rm -rf ${PREFIX}')
29     t.run_cmd('make -j install')
30
31
32 def provision_s4(t, func_level="2008"):
33     '''provision s4 as a DC'''
34     t.info('Provisioning s4')
35     t.chdir('${PREFIX}')
36     t.del_files(["var", "private"])
37     t.run_cmd("rm -f etc/smb.conf")
38     options=' --function-level=%s -d${DEBUGLEVEL}' % func_level
39     options += ' --option=interfaces="${INTERFACE} ${INTERFACE_IPV6}"'
40     options += ' --host-ip=${INTERFACE_IP} --host-ip6=${INTERFACE_IPV6}'
41     options += ' --option=bindinterfacesonly=yes'
42     t.run_cmd('sbin/provision --realm=${LCREALM} --domain=${DOMAIN} --adminpass=${PASSWORD1} --server-role="domain controller"' + options)
43     t.run_cmd('bin/samba-tool newuser testallowed ${PASSWORD1}')
44     t.run_cmd('bin/samba-tool newuser testdenied ${PASSWORD1}')
45     t.run_cmd('bin/samba-tool group addmembers "Allowed RODC Password Replication Group" testallowed')
46
47
48 def start_s4(t):
49     '''startup samba4'''
50     t.info('Starting Samba4')
51     t.chdir("${PREFIX}")
52     t.run_cmd('killall -9 -q samba smbd nmbd winbindd', checkfail=False)
53     t.run_cmd(['sbin/samba',
54              '--option', 'panic action=gnome-terminal -e "gdb --pid %PID%"'])
55     t.port_wait("${INTERFACE_IP}", 139)
56
57 def test_smbclient(t):
58     '''test smbclient'''
59     t.info('Testing smbclient')
60     t.chdir('${PREFIX}')
61     t.cmd_contains("bin/smbclient --version", ["Version 4.0"])
62     t.retry_cmd('bin/smbclient -L ${INTERFACE_IP} -U%', ["netlogon", "sysvol", "IPC Service"])
63     child = t.pexpect_spawn('bin/smbclient //${INTERFACE_IP}/netlogon -Uadministrator%${PASSWORD1}')
64     child.expect("smb:")
65     child.sendline("dir")
66     child.expect("blocks available")
67     child.sendline("mkdir testdir")
68     child.expect("smb:")
69     child.sendline("cd testdir")
70     child.expect('testdir')
71     child.sendline("cd ..")
72     child.sendline("rmdir testdir")
73
74
75 def create_shares(t):
76     '''create some test shares'''
77     t.info("Adding test shares")
78     t.chdir('${PREFIX}')
79     t.write_file("etc/smb.conf", '''
80 [test]
81        path = ${PREFIX}/test
82        read only = no
83 [profiles]
84        path = ${PREFIX}/var/profiles
85        read only = no
86     ''',
87                  mode='a')
88     t.run_cmd("mkdir -p test")
89     t.run_cmd("mkdir -p var/profiles")
90
91
92 def set_nameserver(t, nameserver):
93     '''set the nameserver in resolv.conf'''
94     t.write_file("/etc/resolv.conf.wintest", '''
95 # Generated by wintest, the Samba v Windows automated testing system
96 nameserver %s
97
98 # your original resolv.conf appears below:
99
100 ''' % t.substitute(nameserver))
101     child = t.pexpect_spawn("cat /etc/resolv.conf", crlf=False)
102     i = child.expect(['your original resolv.conf appears below:', pexpect.EOF])
103     if i == 0:
104         child.expect(pexpect.EOF)
105     contents = child.before.replace('\r', '')
106     t.write_file('/etc/resolv.conf.wintest', contents, mode='a')
107     t.write_file('/etc/resolv.conf.wintest-bak', contents)
108     t.run_cmd("mv -f /etc/resolv.conf.wintest /etc/resolv.conf")
109     t.resolv_conf_backup = '/etc/resolv.conf.wintest-bak';
110
111
112 def restore_resolv_conf(t):
113     '''restore the /etc/resolv.conf after testing is complete'''
114     if getattr(t, 'resolv_conf_backup', False):
115         t.info("restoring /etc/resolv.conf")
116         t.run_cmd("mv -f %s /etc/resolv.conf" % t.resolv_conf_backup)
117
118 def rndc_cmd(t, cmd, checkfail=True):
119     '''run a rndc command'''
120     t.run_cmd("${RNDC} -c ${PREFIX}/etc/rndc.conf %s" % cmd, checkfail=checkfail)
121
122
123 def restart_bind(t):
124     '''restart the test environment version of bind'''
125     t.info("Restarting bind9")
126     t.putenv('KEYTAB_FILE', '${PREFIX}/private/dns.keytab')
127     t.putenv('KRB5_KTNAME', '${PREFIX}/private/dns.keytab')
128     t.chdir('${PREFIX}')
129     t.run_cmd("mkdir -p var/named/data")
130     t.run_cmd("chown -R ${BIND_USER} var/named")
131
132     nameserver = t.get_nameserver()
133     if nameserver == t.getvar('INTERFACE_IP'):
134         raise RuntimeError("old /etc/resolv.conf must not contain %s as a nameserver, this will create loops with the generated dns configuration" % nameserver)
135     t.setvar('DNSSERVER', nameserver)
136
137     if t.getvar('INTERFACE_IPV6'):
138         ipv6_listen = 'listen-on-v6 port 53 { ${INTERFACE_IPV6}; };'
139     else:
140         ipv6_listen = ''
141     t.setvar('BIND_LISTEN_IPV6', ipv6_listen)
142
143     t.write_file("etc/named.conf", '''
144 options {
145         listen-on port 53 { ${INTERFACE_IP};  };
146         ${BIND_LISTEN_IPV6}
147         directory       "${PREFIX}/var/named";
148         dump-file       "${PREFIX}/var/named/data/cache_dump.db";
149         pid-file        "${PREFIX}/var/named/named.pid";
150         statistics-file "${PREFIX}/var/named/data/named_stats.txt";
151         memstatistics-file "${PREFIX}/var/named/data/named_mem_stats.txt";
152         allow-query     { any; };
153         recursion yes;
154         tkey-gssapi-credential "DNS/${HOSTNAME}.${LCREALM}";
155         tkey-domain "${REALM}";
156         max-cache-ttl 10;
157         max-ncache-ttl 10;
158
159         forward only;
160         forwarders {
161                   ${DNSSERVER};
162         };
163
164 };
165
166 key "rndc-key" {
167         algorithm hmac-md5;
168         secret "lA/cTrno03mt5Ju17ybEYw==";
169 };
170  
171 controls {
172         inet ${INTERFACE_IP} port 953
173         allow { any; } keys { "rndc-key"; };
174 };
175
176 include "${PREFIX}/private/named.conf";
177 ''')
178
179     # add forwarding for the windows domains
180     domains = t.get_domains()
181     for d in domains:
182         t.write_file('etc/named.conf',
183                      '''
184 zone "%s" IN {
185       type forward;
186       forward only;
187       forwarders {
188          %s;
189       };
190 };
191 ''' % (d, domains[d]),
192                      mode='a')
193
194
195     t.write_file("etc/rndc.conf", '''
196 # Start of rndc.conf
197 key "rndc-key" {
198         algorithm hmac-md5;
199         secret "lA/cTrno03mt5Ju17ybEYw==";
200 };
201
202 options {
203         default-key "rndc-key";
204         default-server  ${INTERFACE_IP};
205         default-port 953;
206 };
207 ''')
208
209     set_nameserver(t, t.getvar('INTERFACE_IP'))
210
211     rndc_cmd(t, "stop", checkfail=False)
212     t.port_wait("${INTERFACE_IP}", 53, wait_for_fail=True)
213     t.bind_child = t.run_child("${BIND9} -u ${BIND_USER} -n 1 -c ${PREFIX}/etc/named.conf -g")
214
215     t.port_wait("${INTERFACE_IP}", 53)
216     rndc_cmd(t, "flush")
217
218
219 def test_dns(t):
220     '''test that DNS is OK'''
221     t.info("Testing DNS")
222     t.cmd_contains("host -t SRV _ldap._tcp.${LCREALM}.",
223                  ['_ldap._tcp.${LCREALM} has SRV record 0 100 389 ${HOSTNAME}.${LCREALM}'])
224     t.cmd_contains("host -t SRV  _kerberos._udp.${LCREALM}.",
225                  ['_kerberos._udp.${LCREALM} has SRV record 0 100 88 ${HOSTNAME}.${LCREALM}'])
226     t.cmd_contains("host -t A ${HOSTNAME}.${LCREALM}",
227                  ['${HOSTNAME}.${LCREALM} has address'])
228
229 def test_kerberos(t):
230     '''test that kerberos is OK'''
231     t.info("Testing kerberos")
232     t.run_cmd("kdestroy")
233     t.kinit("administrator@${REALM}", "${PASSWORD1}")
234     t.cmd_contains("klist -e", ["Ticket cache", "Default principal", "Valid starting"])
235
236
237 def test_dyndns(t):
238     '''test that dynamic DNS is working'''
239     t.chdir('${PREFIX}')
240     t.run_cmd("sbin/samba_dnsupdate --fail-immediately")
241     rndc_cmd(t, "flush")
242
243
244 def run_winjoin(t, vm):
245     '''join a windows box to our domain'''
246     t.setwinvars(vm)
247
248     t.info("Joining a windows box to the domain")
249     t.vm_poweroff("${WIN_VM}", checkfail=False)
250     t.vm_restore("${WIN_VM}", "${WIN_SNAPSHOT}")
251     child = t.open_telnet("${WIN_HOSTNAME}", "${WIN_USER}", "${WIN_PASS}", set_time=True, set_ip=True)
252     child.sendline("netdom join ${WIN_HOSTNAME} /Domain:${LCREALM} /PasswordD:${PASSWORD1} /UserD:administrator")
253     child.expect("The command completed successfully")
254     child.expect("C:")
255     child.sendline("shutdown /r -t 0")
256     t.port_wait("${WIN_IP}", 139, wait_for_fail=True)
257     t.port_wait("${WIN_IP}", 139)
258     child = t.open_telnet("${WIN_HOSTNAME}", "${WIN_USER}", "${WIN_PASS}", set_time=True, set_ip=True)
259     child.sendline("ipconfig /registerdns")
260     child.expect("Registration of the DNS resource records for all adapters of this computer has been initiated. Any errors will be reported in the Event Viewer")
261     child.expect("C:")
262
263 def test_winjoin(t, vm):
264     t.info("Checking the windows join is OK")
265     t.chdir('${PREFIX}')
266     t.port_wait("${WIN_IP}", 139)
267     t.retry_cmd('bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Uadministrator@${LCREALM}%${PASSWORD1}', ["C$", "IPC$", "Sharename"], retries=100)
268     t.cmd_contains("host -t A ${WIN_HOSTNAME}.${LCREALM}.", ['has address'])
269     t.cmd_contains('bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utestallowed@${LCREALM}%${PASSWORD1}', ["C$", "IPC$", "Sharename"])
270     t.cmd_contains('bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -k no -Utestallowed@${LCREALM}%${PASSWORD1}', ["C$", "IPC$", "Sharename"])
271     t.cmd_contains('bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -k yes -Utestallowed@${LCREALM}%${PASSWORD1}', ["C$", "IPC$", "Sharename"])
272     child = t.open_telnet("${WIN_HOSTNAME}", "${DOMAIN}\\administrator", "${PASSWORD1}")
273     child.sendline("net use t: \\\\${HOSTNAME}.${LCREALM}\\test")
274     child.expect("The command completed successfully")
275     t.vm_poweroff("${WIN_VM}")
276
277
278 def run_dcpromo(t, vm):
279     '''run a dcpromo on windows'''
280     t.setwinvars(vm)
281
282     t.info("Joining a windows VM ${WIN_VM} to the domain as a DC using dcpromo")
283     t.vm_poweroff("${WIN_VM}", checkfail=False)
284     t.vm_restore("${WIN_VM}", "${WIN_SNAPSHOT}")
285     child = t.open_telnet("${WIN_HOSTNAME}", "administrator", "${WIN_PASS}", set_ip=True)
286     child.sendline("copy /Y con answers.txt")
287     child.sendline('''
288 [DCINSTALL]
289 RebootOnSuccess=Yes
290 RebootOnCompletion=Yes
291 ReplicaOrNewDomain=Replica
292 ReplicaDomainDNSName=${LCREALM}
293 SiteName=Default-First-Site-Name
294 InstallDNS=No
295 ConfirmGc=Yes
296 CreateDNSDelegation=No
297 UserDomain=${LCREALM}
298 UserName=${LCREALM}\\administrator
299 Password=${PASSWORD1}
300 DatabasePath="C:\Windows\NTDS"
301 LogPath="C:\Windows\NTDS"
302 SYSVOLPath="C:\Windows\SYSVOL"
303 SafeModeAdminPassword=${PASSWORD1}
304 \1a
305 ''')
306     child.expect("copied.")
307     child.expect("C:")
308     child.expect("C:")
309     child.sendline("dcpromo /answer:answers.txt")
310     i = child.expect(["You must restart this computer", "failed", "Active Directory Domain Services was not installed", "C:"], timeout=120)
311     if i == 1 or i == 2:
312         raise Exception("dcpromo failed")
313     t.port_wait("${WIN_IP}", 139, wait_for_fail=True)
314     t.port_wait("${WIN_IP}", 139)
315
316
317 def test_dcpromo(t, vm):
318     '''test that dcpromo worked'''
319     t.info("Checking the dcpromo join is OK")
320     t.chdir('${PREFIX}')
321     t.port_wait("${WIN_IP}", 139)
322     t.retry_cmd('bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Uadministrator@${LCREALM}%${PASSWORD1}', ["C$", "IPC$", "Sharename"])
323     t.cmd_contains("host -t A ${WIN_HOSTNAME}.${LCREALM}.", ['has address'])
324     t.cmd_contains('bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utestallowed@${LCREALM}%${PASSWORD1}', ["C$", "IPC$", "Sharename"])
325
326     t.cmd_contains("bin/samba-tool drs kcc ${HOSTNAME}.${LCREALM} -Uadministrator@${LCREALM}%${PASSWORD1}", ['Consistency check', 'successful'])
327     t.cmd_contains("bin/samba-tool drs kcc ${WIN_HOSTNAME}.${LCREALM} -Uadministrator@${LCREALM}%${PASSWORD1}", ['Consistency check', 'successful'])
328
329     t.kinit("administrator@${REALM}", "${PASSWORD1}")
330     for nc in [ '${BASEDN}', 'CN=Configuration,${BASEDN}', 'CN=Schema,CN=Configuration,${BASEDN}' ]:
331         t.cmd_contains("bin/samba-tool drs replicate ${HOSTNAME}.${LCREALM} ${WIN_HOSTNAME}.${LCREALM} %s -k yes" % nc, ["was successful"])
332         t.cmd_contains("bin/samba-tool drs replicate ${WIN_HOSTNAME}.${LCREALM} ${HOSTNAME}.${LCREALM} %s -k yes" % nc, ["was successful"])
333
334     t.cmd_contains("bin/samba-tool drs showrepl ${HOSTNAME}.${LCREALM} -k yes",
335                  [ "INBOUND NEIGHBORS",
336                    "${BASEDN}",
337                    "Last attempt .* was successful",
338                    "CN=Configuration,${BASEDN}",
339                    "Last attempt .* was successful",
340                    "CN=Configuration,${BASEDN}", # cope with either order
341                    "Last attempt .* was successful",
342                    "OUTBOUND NEIGHBORS",
343                    "${BASEDN}",
344                    "Last success",
345                    "CN=Configuration,${BASEDN}",
346                    "Last success",
347                    "CN=Configuration,${BASEDN}",
348                    "Last success"],
349                    ordered=True,
350                    regex=True)
351
352     t.cmd_contains("bin/samba-tool drs showrepl ${WIN_HOSTNAME}.${LCREALM} -k yes",
353                  [ "INBOUND NEIGHBORS",
354                    "${BASEDN}",
355                    "Last attempt .* was successful",
356                    "CN=Configuration,${BASEDN}",
357                    "Last attempt .* was successful",
358                    "CN=Configuration,${BASEDN}",
359                    "Last attempt .* was successful",
360                    "OUTBOUND NEIGHBORS",
361                    "${BASEDN}",
362                    "Last success",
363                    "CN=Configuration,${BASEDN}",
364                    "Last success",
365                    "CN=Configuration,${BASEDN}",
366                    "Last success" ],
367                    ordered=True,
368                    regex=True)
369
370     child = t.open_telnet("${WIN_HOSTNAME}", "${DOMAIN}\\administrator", "${PASSWORD1}", set_time=True)
371     child.sendline("net use t: \\\\${HOSTNAME}.${LCREALM}\\test")
372     child.expect("The command completed successfully")
373
374     t.run_net_time(child)
375
376     t.info("Checking if showrepl is happy")
377     child.sendline("repadmin /showrepl")
378     child.expect("${BASEDN}")
379     child.expect("was successful")
380     child.expect("CN=Configuration,${BASEDN}")
381     child.expect("was successful")
382     child.expect("CN=Schema,CN=Configuration,${BASEDN}")
383     child.expect("was successful")
384
385     t.info("Checking if new users propogate to windows")
386     t.run_cmd('bin/samba-tool newuser test2 ${PASSWORD2}')
387     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utest2%${PASSWORD2} -k no", ['Sharename', 'Remote IPC'])
388     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utest2%${PASSWORD2} -k yes", ['Sharename', 'Remote IPC'])
389
390     t.info("Checking if new users on windows propogate to samba")
391     child.sendline("net user test3 ${PASSWORD3} /add")
392     while True:
393         i = child.expect(["The command completed successfully",
394                           "The directory service was unable to allocate a relative identifier"])
395         if i == 0:
396             break
397         time.sleep(2)
398
399     t.retry_cmd("bin/smbclient -L ${HOSTNAME}.${LCREALM} -Utest3%${PASSWORD3} -k no", ['Sharename', 'IPC'])
400     t.retry_cmd("bin/smbclient -L ${HOSTNAME}.${LCREALM} -Utest3%${PASSWORD3} -k yes", ['Sharename', 'IPC'])
401
402     t.info("Checking propogation of user deletion")
403     t.run_cmd('bin/samba-tool user delete test2 -Uadministrator@${LCREALM}%${PASSWORD1}')
404     child.sendline("net user test3 /del")
405     child.expect("The command completed successfully")
406
407     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utest2%${PASSWORD2} -k no", ['LOGON_FAILURE'])
408     t.retry_cmd("bin/smbclient -L ${HOSTNAME}.${LCREALM} -Utest3%${PASSWORD3} -k no", ['LOGON_FAILURE'])
409     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utest2%${PASSWORD2} -k yes", ['LOGON_FAILURE'])
410     t.retry_cmd("bin/smbclient -L ${HOSTNAME}.${LCREALM} -Utest3%${PASSWORD3} -k yes", ['LOGON_FAILURE'])
411     t.vm_poweroff("${WIN_VM}")
412
413
414 def run_dcpromo_rodc(t, vm):
415     '''run a RODC dcpromo to join a windows DC to the samba domain'''
416     t.setwinvars(vm)
417     t.info("Joining a w2k8 box to the domain as a RODC")
418     t.vm_poweroff("${WIN_VM}", checkfail=False)
419     t.vm_restore("${WIN_VM}", "${WIN_SNAPSHOT}")
420     child = t.open_telnet("${WIN_HOSTNAME}", "administrator", "${WIN_PASS}", set_ip=True)
421     child.sendline("copy /Y con answers.txt")
422     child.sendline('''
423 [DCInstall]
424 ReplicaOrNewDomain=ReadOnlyReplica
425 ReplicaDomainDNSName=${LCREALM}
426 PasswordReplicationDenied="BUILTIN\Administrators"
427 PasswordReplicationDenied="BUILTIN\Server Operators"
428 PasswordReplicationDenied="BUILTIN\Backup Operators"
429 PasswordReplicationDenied="BUILTIN\Account Operators"
430 PasswordReplicationDenied="${DOMAIN}\Denied RODC Password Replication Group"
431 PasswordReplicationAllowed="${DOMAIN}\Allowed RODC Password Replication Group"
432 DelegatedAdmin="${DOMAIN}\\Administrator"
433 SiteName=Default-First-Site-Name
434 InstallDNS=No
435 ConfirmGc=Yes
436 CreateDNSDelegation=No
437 UserDomain=${LCREALM}
438 UserName=${LCREALM}\\administrator
439 Password=${PASSWORD1}
440 DatabasePath="C:\Windows\NTDS"
441 LogPath="C:\Windows\NTDS"
442 SYSVOLPath="C:\Windows\SYSVOL"
443 SafeModeAdminPassword=${PASSWORD1}
444 RebootOnCompletion=No
445 \1a
446 ''')
447     child.expect("copied.")
448     child.sendline("dcpromo /answer:answers.txt")
449     i = child.expect(["You must restart this computer", "failed"], timeout=120)
450     if i != 0:
451         raise Exception("dcpromo failed")
452     child.sendline("shutdown -r -t 0")
453     t.port_wait("${WIN_IP}", 139, wait_for_fail=True)
454     t.port_wait("${WIN_IP}", 139)
455
456
457
458 def test_dcpromo_rodc(t, vm):
459     '''test the RODC dcpromo worked'''
460     t.info("Checking the w2k8 RODC join is OK")
461     t.chdir('${PREFIX}')
462     t.port_wait("${WIN_IP}", 139)
463     t.retry_cmd('bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Uadministrator@${LCREALM}%${PASSWORD1}', ["C$", "IPC$", "Sharename"])
464     t.cmd_contains("host -t A ${WIN_HOSTNAME}.${LCREALM}.", ['has address'])
465     t.cmd_contains('bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utestallowed@${LCREALM}%${PASSWORD1}', ["C$", "IPC$", "Sharename"])
466     child = t.open_telnet("${WIN_HOSTNAME}", "${DOMAIN}\\administrator", "${PASSWORD1}", set_time=True)
467     child.sendline("net use t: \\\\${HOSTNAME}.${LCREALM}\\test")
468     child.expect("The command completed successfully")
469
470     t.info("Checking if showrepl is happy")
471     child.sendline("repadmin /showrepl")
472     child.expect("${BASEDN}")
473     child.expect("was successful")
474     child.expect("CN=Configuration,${BASEDN}")
475     child.expect("was successful")
476     child.expect("CN=Configuration,${BASEDN}")
477     child.expect("was successful")
478
479     t.info("Checking if new users are available on windows")
480     t.run_cmd('bin/samba-tool newuser test2 ${PASSWORD2}')
481     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utest2%${PASSWORD2} -k yes", ['Sharename', 'Remote IPC'])
482     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utest2%${PASSWORD2} -k no", ['LOGON_FAILURE'])
483     t.retry_cmd("bin/samba-tool drs replicate ${WIN_HOSTNAME}.${LCREALM} ${HOSTNAME}.${LCREALM} ${BASEDN} -k yes", ["was successful"])
484     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utest2%${PASSWORD2} -k no", ['Sharename', 'Remote IPC'])
485     t.run_cmd('bin/samba-tool user delete test2 -Uadministrator@${LCREALM}%${PASSWORD1}')
486     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utest2%${PASSWORD2} -k yes", ['LOGON_FAILURE'])
487     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utest2%${PASSWORD2} -k no", ['LOGON_FAILURE'])
488     t.vm_poweroff("${WIN_VM}")
489
490
491 def join_as_dc(t, vm):
492     '''join a windows domain as a DC'''
493     t.setwinvars(vm)
494     t.info("Joining ${WIN_VM} as a second DC using samba-tool join DC")
495     t.chdir('${PREFIX}')
496     t.run_cmd('killall -9 -q samba smbd nmbd winbindd', checkfail=False)
497     t.vm_poweroff("${WIN_VM}", checkfail=False)
498     t.vm_restore("${WIN_VM}", "${WIN_SNAPSHOT}")
499     rndc_cmd(t, 'flush')
500     t.run_cmd("rm -rf etc/smb.conf private")
501     child = t.open_telnet("${WIN_HOSTNAME}", "${WIN_DOMAIN}\\administrator", "${WIN_PASS}", set_time=True)
502     t.get_ipconfig(child)
503     t.retry_cmd("bin/samba-tool drs showrepl ${WIN_HOSTNAME}.${WIN_REALM} -Uadministrator%${WIN_PASS}", ['INBOUND NEIGHBORS'] )
504     t.run_cmd('bin/samba-tool join ${WIN_REALM} DC -Uadministrator%${WIN_PASS} -d${DEBUGLEVEL} --option=interfaces=${INTERFACE}')
505     t.run_cmd('bin/samba-tool drs kcc ${WIN_HOSTNAME}.${WIN_REALM} -Uadministrator@${WIN_REALM}%${WIN_PASS}')
506
507
508 def test_join_as_dc(t, vm):
509     '''test the join of a windows domain as a DC'''
510     t.info("Checking the DC join is OK")
511     t.chdir('${PREFIX}')
512     t.retry_cmd('bin/smbclient -L ${HOSTNAME}.${WIN_REALM} -Uadministrator@${WIN_REALM}%${WIN_PASS}', ["C$", "IPC$", "Sharename"])
513     t.cmd_contains("host -t A ${HOSTNAME}.${WIN_REALM}.", ['has address'])
514     child = t.open_telnet("${WIN_HOSTNAME}", "${WIN_DOMAIN}\\administrator", "${WIN_PASS}", set_time=True)
515
516     t.info("Forcing kcc runs, and replication")
517     t.run_cmd('bin/samba-tool drs kcc ${WIN_HOSTNAME}.${WIN_REALM} -Uadministrator@${WIN_REALM}%${WIN_PASS}')
518     t.run_cmd('bin/samba-tool drs kcc ${HOSTNAME}.${WIN_REALM} -Uadministrator@${WIN_REALM}%${WIN_PASS}')
519
520     t.kinit("administrator@${WIN_REALM}", "${WIN_PASS}")
521     for nc in [ '${WIN_BASEDN}', 'CN=Configuration,${WIN_BASEDN}', 'CN=Schema,CN=Configuration,${WIN_BASEDN}' ]:
522         t.cmd_contains("bin/samba-tool drs replicate ${HOSTNAME}.${WIN_REALM} ${WIN_HOSTNAME}.${WIN_REALM} %s -k yes" % nc, ["was successful"])
523         t.cmd_contains("bin/samba-tool drs replicate ${WIN_HOSTNAME}.${WIN_REALM} ${HOSTNAME}.${WIN_REALM} %s -k yes" % nc, ["was successful"])
524
525     child.sendline("net use t: \\\\${HOSTNAME}.${WIN_REALM}\\test")
526     child.expect("The command completed successfully")
527
528     t.info("Checking if showrepl is happy")
529     child.sendline("repadmin /showrepl")
530     child.expect("${WIN_BASEDN}")
531     child.expect("was successful")
532     child.expect("CN=Configuration,${WIN_BASEDN}")
533     child.expect("was successful")
534     child.expect("CN=Configuration,${WIN_BASEDN}")
535     child.expect("was successful")
536
537     t.info("Checking if new users propogate to windows")
538     t.run_cmd('bin/samba-tool newuser test2 ${PASSWORD2}')
539     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${WIN_REALM} -Utest2%${PASSWORD2} -k no", ['Sharename', 'Remote IPC'])
540     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${WIN_REALM} -Utest2%${PASSWORD2} -k yes", ['Sharename', 'Remote IPC'])
541
542     t.info("Checking if new users on windows propogate to samba")
543     child.sendline("net user test3 ${PASSWORD3} /add")
544     child.expect("The command completed successfully")
545     t.retry_cmd("bin/smbclient -L ${HOSTNAME}.${WIN_REALM} -Utest3%${PASSWORD3} -k no", ['Sharename', 'IPC'])
546     t.retry_cmd("bin/smbclient -L ${HOSTNAME}.${WIN_REALM} -Utest3%${PASSWORD3} -k yes", ['Sharename', 'IPC'])
547
548     t.info("Checking propogation of user deletion")
549     t.run_cmd('bin/samba-tool user delete test2 -Uadministrator@${WIN_REALM}%${WIN_PASS}')
550     child.sendline("net user test3 /del")
551     child.expect("The command completed successfully")
552
553     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${WIN_REALM} -Utest2%${PASSWORD2} -k no", ['LOGON_FAILURE'])
554     t.retry_cmd("bin/smbclient -L ${HOSTNAME}.${WIN_REALM} -Utest3%${PASSWORD3} -k no", ['LOGON_FAILURE'])
555     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${WIN_REALM} -Utest2%${PASSWORD2} -k yes", ['LOGON_FAILURE'])
556     t.retry_cmd("bin/smbclient -L ${HOSTNAME}.${WIN_REALM} -Utest3%${PASSWORD3} -k yes", ['LOGON_FAILURE'])
557     t.vm_poweroff("${WIN_VM}")
558
559
560 def join_as_rodc(t, vm):
561     '''join a windows domain as a RODC'''
562     t.setwinvars(vm)
563     t.info("Joining ${WIN_VM} as a RODC using samba-tool join DC")
564     t.chdir('${PREFIX}')
565     t.run_cmd('killall -9 -q samba smbd nmbd winbindd', checkfail=False)
566     t.vm_poweroff("${WIN_VM}", checkfail=False)
567     t.vm_restore("${WIN_VM}", "${WIN_SNAPSHOT}")
568     rndc_cmd(t, 'flush')
569     t.run_cmd("rm -rf etc/smb.conf private")
570     child = t.open_telnet("${WIN_HOSTNAME}", "${WIN_DOMAIN}\\administrator", "${WIN_PASS}", set_time=True)
571     t.get_ipconfig(child)
572     t.retry_cmd("bin/samba-tool drs showrepl ${WIN_HOSTNAME}.${WIN_REALM} -Uadministrator%${WIN_PASS}", ['INBOUND NEIGHBORS'] )
573     t.run_cmd('bin/samba-tool join ${WIN_REALM} RODC -Uadministrator%${WIN_PASS} -d${DEBUGLEVEL} --option=interfaces=${INTERFACE}')
574     t.run_cmd('bin/samba-tool drs kcc ${WIN_HOSTNAME}.${WIN_REALM} -Uadministrator@${WIN_REALM}%${WIN_PASS}')
575
576
577 def test_join_as_rodc(t, vm):
578     '''test a windows domain RODC join'''
579     t.info("Checking the RODC join is OK")
580     t.chdir('${PREFIX}')
581     t.retry_cmd('bin/smbclient -L ${HOSTNAME}.${WIN_REALM} -Uadministrator@${WIN_REALM}%${WIN_PASS}', ["C$", "IPC$", "Sharename"])
582     t.cmd_contains("host -t A ${HOSTNAME}.${WIN_REALM}.", ['has address'])
583     child = t.open_telnet("${WIN_HOSTNAME}", "${WIN_DOMAIN}\\administrator", "${WIN_PASS}", set_time=True)
584
585     t.info("Forcing kcc runs, and replication")
586     t.run_cmd('bin/samba-tool drs kcc ${HOSTNAME}.${WIN_REALM} -Uadministrator@${WIN_REALM}%${WIN_PASS}')
587     t.run_cmd('bin/samba-tool drs kcc ${WIN_HOSTNAME}.${WIN_REALM} -Uadministrator@${WIN_REALM}%${WIN_PASS}')
588
589     t.kinit("administrator@${WIN_REALM}", "${WIN_PASS}")
590     for nc in [ '${WIN_BASEDN}', 'CN=Configuration,${WIN_BASEDN}', 'CN=Schema,CN=Configuration,${WIN_BASEDN}' ]:
591         t.cmd_contains("bin/samba-tool drs replicate ${HOSTNAME}.${WIN_REALM} ${WIN_HOSTNAME}.${WIN_REALM} %s -k yes" % nc, ["was successful"])
592
593     child.sendline("net use t: \\\\${HOSTNAME}.${WIN_REALM}\\test")
594     child.expect("The command completed successfully")
595
596     t.info("Checking if showrepl is happy")
597     child.sendline("repadmin /showrepl")
598     child.expect("DSA invocationID")
599
600     t.cmd_contains("bin/samba-tool drs showrepl ${WIN_HOSTNAME}.${WIN_REALM} -k yes",
601                  [ "INBOUND NEIGHBORS",
602                    "OUTBOUND NEIGHBORS",
603                    "${WIN_BASEDN}",
604                    "Last attempt .* was successful",
605                    "CN=Configuration,${WIN_BASEDN}",
606                    "Last attempt .* was successful",
607                    "CN=Configuration,${WIN_BASEDN}",
608                    "Last attempt .* was successful" ],
609                    ordered=True,
610                    regex=True)
611
612     t.info("Checking if new users on windows propogate to samba")
613     child.sendline("net user test3 ${PASSWORD3} /add")
614     child.expect("The command completed successfully")
615     t.retry_cmd("bin/smbclient -L ${HOSTNAME}.${WIN_REALM} -Utest3%${PASSWORD3} -k no", ['Sharename', 'IPC'])
616     t.retry_cmd("bin/smbclient -L ${HOSTNAME}.${WIN_REALM} -Utest3%${PASSWORD3} -k yes", ['Sharename', 'IPC'])
617
618     # should this work?
619     t.info("Checking if new users propogate to windows")
620     t.cmd_contains('bin/samba-tool newuser test2 ${PASSWORD2}', ['No RID Set DN'])
621
622     t.info("Checking propogation of user deletion")
623     child.sendline("net user test3 /del")
624     child.expect("The command completed successfully")
625
626     t.retry_cmd("bin/smbclient -L ${HOSTNAME}.${WIN_REALM} -Utest3%${PASSWORD3} -k no", ['LOGON_FAILURE'])
627     t.retry_cmd("bin/smbclient -L ${HOSTNAME}.${WIN_REALM} -Utest3%${PASSWORD3} -k yes", ['LOGON_FAILURE'])
628     t.vm_poweroff("${WIN_VM}")
629
630
631 def test_howto(t):
632     '''test the Samba4 howto'''
633
634     check_prerequesites(t)
635
636     # we don't need fsync safety in these tests
637     t.putenv('TDB_NO_FSYNC', '1')
638
639     if not t.skip("build"):
640         build_s4(t)
641
642     if not t.skip("provision"):
643         provision_s4(t)
644
645     if not t.skip("create-shares"):
646         create_shares(t)
647
648     if not t.skip("starts4"):
649         start_s4(t)
650     if not t.skip("smbclient"):
651         test_smbclient(t)
652     if not t.skip("startbind"):
653         restart_bind(t)
654     if not t.skip("dns"):
655         test_dns(t)
656     if not t.skip("kerberos"):
657         test_kerberos(t)
658     if not t.skip("dyndns"):
659         test_dyndns(t)
660
661     if t.have_var('WINDOWS7_VM') and not t.skip("windows7"):
662         run_winjoin(t, "WINDOWS7")
663         test_winjoin(t, "WINDOWS7")
664
665     if t.have_var('WINXP_VM') and not t.skip("winxp"):
666         run_winjoin(t, "WINXP")
667         test_winjoin(t, "WINXP")
668
669     if t.have_var('W2K8R2C_VM') and not t.skip("dcpromo_rodc"):
670         t.info("Testing w2k8r2 RODC dcpromo")
671         run_dcpromo_rodc(t, "W2K8R2C")
672         test_dcpromo_rodc(t, "W2K8R2C")
673
674     if t.have_var('W2K8R2B_VM') and not t.skip("dcpromo_w2k8r2"):
675         t.info("Testing w2k8r2 dcpromo")
676         run_dcpromo(t, "W2K8R2B")
677         test_dcpromo(t, "W2K8R2B")
678
679     if t.have_var('W2K8B_VM') and not t.skip("dcpromo_w2k8"):
680         t.info("Testing w2k8 dcpromo")
681         run_dcpromo(t, "W2K8B")
682         test_dcpromo(t, "W2K8B")
683
684     if t.have_var('W2K3B_VM') and not t.skip("dcpromo_w2k3"):
685         t.info("Testing w2k3 dcpromo")
686         t.info("Changing to 2003 functional level")
687         provision_s4(t, func_level='2003')
688         create_shares(t)
689         start_s4(t)
690         test_smbclient(t)
691         restart_bind(t)
692         test_dns(t)
693         test_kerberos(t)
694         test_dyndns(t)
695         run_dcpromo(t, "W2K3B")
696         test_dcpromo(t, "W2K3B")
697
698     if t.have_var('W2K8R2A_VM') and not t.skip("join_w2k8r2"):
699         join_as_dc(t, "W2K8R2A")
700         create_shares(t)
701         start_s4(t)
702         test_dyndns(t)
703         test_join_as_dc(t, "W2K8R2A")
704
705     if t.have_var('W2K8R2A_VM') and not t.skip("join_rodc"):
706         join_as_rodc(t, "W2K8R2A")
707         create_shares(t)
708         start_s4(t)
709         test_dyndns(t)
710         test_join_as_rodc(t, "W2K8R2A")
711
712     if t.have_var('W2K3A_VM') and not t.skip("join_w2k3"):
713         join_as_dc(t, "W2K3A")
714         create_shares(t)
715         start_s4(t)
716         test_dyndns(t)
717         test_join_as_dc(t, "W2K3A")
718
719     t.info("Howto test: All OK")
720
721
722 def test_cleanup(t):
723     '''cleanup after tests'''
724     t.info("Cleaning up ...")
725     restore_resolv_conf(t)
726     if getattr(t, 'bind_child', False):
727         t.bind_child.kill()
728
729
730 if __name__ == '__main__':
731     parser = optparse.OptionParser("test-howto.py")
732     parser.add_option("--conf", type='string', default='', help='config file')
733     parser.add_option("--skip", type='string', default='', help='list of steps to skip (comma separated)')
734     parser.add_option("--list", action='store_true', default=False, help='list the available steps')
735     parser.add_option("--rebase", action='store_true', default=False, help='do a git pull --rebase')
736     parser.add_option("--clean", action='store_true', default=False, help='clean the tree')
737     parser.add_option("--prefix", type='string', default=None, help='override install prefix')
738     parser.add_option("--sourcetree", type='string', default=None, help='override sourcetree location')
739     parser.add_option("--nocleanup", action='store_true', default=False, help='disable cleanup code')
740
741     opts, args = parser.parse_args()
742
743     if not opts.conf:
744         print("Please specify a config file with --conf")
745         sys.exit(1)
746
747     t = wintest.wintest()
748     t.load_config(opts.conf)
749     t.set_skip(opts.skip)
750
751     if opts.list:
752         t.list_steps_mode()
753
754     if opts.prefix:
755         t.setvar('PREFIX', opts.prefix)
756
757     if opts.sourcetree:
758         t.setvar('SOURCETREE', opts.sourcetree)
759
760     if opts.rebase:
761         t.info('rebasing')
762         t.chdir('${SOURCETREE}')
763         t.run_cmd('git pull --rebase')
764
765     if opts.clean:
766         t.info('rebasing')
767         t.chdir('${SOURCETREE}/source4')
768         t.run_cmd('rm -rf bin')
769
770     try:
771         test_howto(t)
772     except:
773         if not opts.nocleanup:
774             test_cleanup(t)
775         raise
776
777     if not opts.nocleanup:
778         test_cleanup(t)
779     t.info("S4 howto test: All OK")