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