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