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