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