PEP8: fix E703: statement ends with a semicolon
[nivanova/samba-autobuild/.git] / wintest / test-s3.py
1 #!/usr/bin/env python
2
3 '''automated testing of Samba3 against windows'''
4
5 import sys
6 import os
7 import optparse
8 import wintest
9
10
11 def set_libpath(t):
12     t.putenv("LD_LIBRARY_PATH", "${PREFIX}/lib")
13
14
15 def set_krb5_conf(t):
16     t.run_cmd("mkdir -p ${PREFIX}/etc")
17     t.write_file("${PREFIX}/etc/krb5.conf", 
18                  '''[libdefaults]
19         dns_lookup_realm = false
20         dns_lookup_kdc = true''')
21
22     t.putenv("KRB5_CONFIG", '${PREFIX}/etc/krb5.conf')
23
24
25 def build_s3(t):
26     '''build samba3'''
27     t.info('Building s3')
28     t.chdir('${SOURCETREE}/source3')
29     t.putenv('CC', 'ccache gcc')
30     t.run_cmd("./autogen.sh")
31     t.run_cmd("./configure -C --prefix=${PREFIX} --enable-developer")
32     t.run_cmd('make basics')
33     t.run_cmd('make -j4')
34     t.run_cmd('rm -rf ${PREFIX}')
35     t.run_cmd('make install')
36
37
38 def start_s3(t):
39     t.info('Starting Samba3')
40     t.chdir("${PREFIX}")
41     t.run_cmd('killall -9 -q samba smbd nmbd winbindd', checkfail=False)
42     t.run_cmd("rm -f var/locks/*.pid")
43     t.run_cmd(['sbin/nmbd', "-D"])
44     t.run_cmd(['sbin/winbindd', "-D"])
45     t.run_cmd(['sbin/smbd', "-D"])
46     t.port_wait("${INTERFACE_IP}", 139)
47
48
49 def test_wbinfo(t):
50     t.info('Testing wbinfo')
51     t.chdir('${PREFIX}')
52     t.cmd_contains("bin/wbinfo --version", ["Version 4."])
53     t.cmd_contains("bin/wbinfo -p", ["Ping to winbindd succeeded"])
54     t.retry_cmd("bin/wbinfo --online-status",
55                 ["BUILTIN : online",
56                  "${HOSTNAME} : online",
57                  "${WIN_DOMAIN} : online"],
58                 casefold=True)
59     t.cmd_contains("bin/wbinfo -u",
60                    ["${WIN_DOMAIN}/administrator",
61                     "${WIN_DOMAIN}/krbtgt"],
62                    casefold=True)
63     t.cmd_contains("bin/wbinfo -g",
64                    ["${WIN_DOMAIN}/domain users",
65                     "${WIN_DOMAIN}/domain guests",
66                     "${WIN_DOMAIN}/domain admins"],
67                    casefold=True)
68     t.cmd_contains("bin/wbinfo --name-to-sid administrator",
69                    "S-1-5-.*-500 SID_USER .1",
70                    regex=True)
71     t.cmd_contains("bin/wbinfo --name-to-sid 'domain users'",
72                    "S-1-5-.*-513 SID_DOM_GROUP .2",
73                    regex=True)
74
75     t.retry_cmd("bin/wbinfo --authenticate=${WIN_DOMAIN}/administrator%${WIN_PASS}",
76                 ["plaintext password authentication succeeded",
77                  "challenge/response password authentication succeeded"])
78
79     t.retry_cmd("bin/wbinfo --krb5auth=${WIN_DOMAIN}/administrator%${WIN_PASS}",
80                 ["succeeded"])
81
82
83 def test_smbclient(t):
84     t.info('Testing smbclient')
85     smbclient = t.getvar("smbclient")
86     t.chdir('${PREFIX}')
87     t.cmd_contains("%s --version" % (smbclient), ["Version 4."])
88     t.cmd_contains('%s -L ${INTERFACE_IP} -U%%' % (smbclient), ["Domain=[${WIN_DOMAIN}]", "test", "IPC$", "Samba 4."],
89                    casefold=True)
90     child = t.pexpect_spawn('%s //${HOSTNAME}.${WIN_REALM}/test -Uroot@${WIN_REALM}%%${PASSWORD2}' % (smbclient))
91     child.expect("smb:")
92     child.sendline("dir")
93     child.expect("blocks available")
94     child.sendline("mkdir testdir")
95     child.expect("smb:")
96     child.sendline("cd testdir")
97     child.expect('testdir')
98     child.sendline("cd ..")
99     child.sendline("rmdir testdir")
100
101     child = t.pexpect_spawn('%s //${HOSTNAME}.${WIN_REALM}/test -Uroot@${WIN_REALM}%%${PASSWORD2} -k' % (smbclient))
102     child.expect("smb:")
103     child.sendline("dir")
104     child.expect("blocks available")
105     child.sendline("mkdir testdir")
106     child.expect("smb:")
107     child.sendline("cd testdir")
108     child.expect('testdir')
109     child.sendline("cd ..")
110     child.sendline("rmdir testdir")
111
112
113 def create_shares(t):
114     t.info("Adding test shares")
115     t.chdir('${PREFIX}')
116     t.write_file("etc/smb.conf", '''
117 [test]
118        path = ${PREFIX}/test
119        read only = no
120        ''',
121                  mode='a')
122     t.run_cmd("mkdir -p test")
123
124
125 def prep_join_as_member(t, vm):
126     '''prepare to join a windows domain as a member server'''
127     t.setwinvars(vm)
128     t.info("Starting VMs for joining ${WIN_VM} as a member using net ads join")
129     t.chdir('${PREFIX}')
130     t.run_cmd('killall -9 -q samba smbd nmbd winbindd', checkfail=False)
131     t.vm_poweroff("${WIN_VM}", checkfail=False)
132     t.vm_restore("${WIN_VM}", "${WIN_SNAPSHOT}")
133     child = t.open_telnet("${WIN_HOSTNAME}", "administrator", "${WIN_PASS}", set_time=True)
134     t.get_ipconfig(child)
135     t.del_files(["var", "private"])
136     t.write_file("etc/smb.conf", '''
137 [global]
138         netbios name = ${HOSTNAME}
139         log level = ${DEBUGLEVEL}
140         realm = ${WIN_REALM}
141         workgroup = ${WIN_DOMAIN}
142         security = ADS
143         bind interfaces only = yes
144         interfaces = ${INTERFACE}
145         winbind separator = /
146         idmap uid = 1000000-2000000
147         idmap gid = 1000000-2000000
148         winbind enum users = yes
149         winbind enum groups = yes
150         max protocol = SMB2
151         map hidden = no
152         map system = no
153         ea support = yes
154         panic action = xterm -e gdb --pid %d
155     ''')
156
157
158 def join_as_member(t, vm):
159     '''join a windows domain as a member server'''
160     t.setwinvars(vm)
161     t.info("Joining ${WIN_VM} as a member using net ads join")
162     t.port_wait("${WIN_IP}", 389)
163     t.retry_cmd("host -t SRV _ldap._tcp.${WIN_REALM} ${WIN_IP}", ['has SRV record'])
164     t.cmd_contains("bin/net ads join -Uadministrator%${WIN_PASS}", ["Joined"])
165     t.cmd_contains("bin/net ads testjoin", ["Join is OK"])
166     t.cmd_contains("bin/net ads dns register ${HOSTNAME}.${WIN_REALM} -P", ["Successfully registered hostname with DNS"])
167     t.cmd_contains("host -t A ${HOSTNAME}.${WIN_REALM}",
168                    ['${HOSTNAME}.${WIN_REALM} has address'])
169
170
171 def create_root_account(t, vm):
172     t.setwinvars(vm)
173     t.info("Creating 'root' account for testing Samba3 member server")
174     t.chdir('${PREFIX}')
175     t.run_cmd('bin/net ads user add root -Uadministrator%${WIN_PASS}')
176     child = t.pexpect_spawn('bin/net ads password root -Uadministrator%${WIN_PASS}')
177     child.expect("Enter new password for root")
178     child.sendline("${PASSWORD2}")
179     child.expect("Password change for ")
180     child.expect(" completed")
181     child = t.pexpect_spawn('bin/net rpc shell -S ${WIN_HOSTNAME}.${WIN_REALM} -Uadministrator%${WIN_PASS}')
182     child.expect("net rpc>")
183     child.sendline("user edit disabled root no")
184     child.expect("Set root's disabled flag")
185
186
187 def test_join_as_member(t, vm):
188     '''test the domain join'''
189     t.setwinvars(vm)
190     t.info('Testing join as member')
191     t.chdir('${PREFIX}')
192     test_wbinfo(t)
193     test_smbclient(t)
194
195
196 def test_s3(t):
197     '''basic s3 testing'''
198
199     t.setvar("SAMBA_VERSION", "Version 4")
200     t.setvar("smbclient", "bin/smbclient")
201     t.check_prerequesites()
202     set_libpath(t)
203
204     if not t.skip("configure_bind"):
205         t.configure_bind()
206     if not t.skip("stop_bind"):
207         t.stop_bind()
208     if not t.skip("stop_vms"):
209         t.stop_vms()
210
211     if not t.skip("build"):
212         build_s3(t)
213
214     set_krb5_conf(t)
215     if not t.skip("configure_bind2"):
216         t.configure_bind()
217     if not t.skip("start_bind"):
218         t.start_bind()
219
220     dc_started = False
221     if t.have_var('W2K8R2A_VM') and not t.skip("join_w2k8r2"):
222         t.start_winvm('W2K8R2A')
223         dc_started = True
224         prep_join_as_member(t, "W2K8R2A")
225         t.run_dcpromo_as_first_dc("W2K8R2A", func_level='2008r2')
226         join_as_member(t, "W2K8R2A")
227         create_shares(t)
228         start_s3(t)
229         create_root_account(t, "W2K8R2A")
230         test_join_as_member(t, "W2K8R2A")
231
232     if t.have_var('WINDOWS7_VM') and t.have_var('W2K8R2A_VM') and not t.skip("join_windows7_2008r2"):
233         if not dc_started:
234             t.start_winvm('W2K8R2A')
235             t.run_dcpromo_as_first_dc("W2K8R2A", func_level='2008r2')
236             dc_started = True
237         else:
238             t.setwinvars('W2K8R2A')
239         realm = t.getvar("WIN_REALM")
240         dom_username = t.getvar("WIN_USER")
241         dom_password = t.getvar("WIN_PASS")
242         dom_realm = t.getvar("WIN_REALM")
243         t.start_winvm('WINDOWS7')
244         t.test_remote_smbclient("WINDOWS7")
245         t.run_winjoin('WINDOWS7', realm, username=dom_username, password=dom_password)
246         t.test_remote_smbclient("WINDOWS7", dom_username, dom_password)
247         t.test_remote_smbclient('WINDOWS7', dom_username, dom_password, args='--option=clientntlmv2auth=no')
248         t.test_remote_smbclient('WINDOWS7', "%s@%s" % (dom_username, dom_realm), dom_password, args="-k")
249         t.test_remote_smbclient('WINDOWS7', "%s@%s" % (dom_username, dom_realm), dom_password, args="-k --option=clientusespnegoprincipal=yes")
250         t.test_net_use('WINDOWS7', dom_realm, t.getvar("W2K8R2A_DOMAIN"), 'root', '${PASSWORD2}')
251
252     if t.have_var('WINXP_VM') and t.have_var('W2K8R2A_VM') and not t.skip("join_winxp_2008r2"):
253         if not dc_started:
254             t.start_winvm('W2K8R2A')
255             t.run_dcpromo_as_first_dc("W2K8R2A", func_level='2008r2')
256             dc_started = True
257         else:
258             t.setwinvars('W2K8R2A')
259         realm = t.getvar("WIN_REALM")
260         dom_username = t.getvar("WIN_USER")
261         dom_password = t.getvar("WIN_PASS")
262         dom_realm = t.getvar("WIN_REALM")
263         t.start_winvm('WINXP')
264         t.run_winjoin('WINXP', realm, username=dom_username, password=dom_password)
265         t.test_remote_smbclient('WINXP', dom_username, dom_password)
266         t.test_remote_smbclient('WINXP', dom_username, dom_password, args='--option=clientntlmv2auth=no')
267         t.test_remote_smbclient('WINXP', "%s@%s" % (dom_username, dom_realm), dom_password, args="-k")
268         t.test_remote_smbclient('WINXP', "%s@%s" % (dom_username, dom_realm), dom_password, args="-k --clientusespnegoprincipal=yes")
269         t.test_net_use('WINXP', dom_realm, t.getvar("W2K8R2A_DOMAIN"), 'root', '${PASSWORD2}')
270
271     t.info("S3 test: All OK")
272
273
274 def test_cleanup(t):
275     '''cleanup after tests'''
276     t.info("Cleaning up ...")
277     t.restore_resolv_conf()
278     if getattr(t, 'bind_child', False):
279         t.bind_child.kill()
280
281
282 if __name__ == '__main__':
283     t = wintest.wintest()
284
285     t.setup("test-s3.py", "source3")
286
287     try:
288         test_s3(t)
289     except:
290         if not t.opts.nocleanup:
291             test_cleanup(t)
292         raise
293
294     if not t.opts.nocleanup:
295         test_cleanup(t)
296     t.info("S3 test: All OK")