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