PEP8: fix E226: missing whitespace around arithmetic operator
[nivanova/samba-autobuild/.git] / python / samba / tests / dcerpc / raw_protocol.py
1 #!/usr/bin/env python
2 # Unix SMB/CIFS implementation.
3 # Copyright (C) Stefan Metzmacher 2014,2015
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 import sys
20 import os
21 import time
22
23 sys.path.insert(0, "bin/python")
24 os.environ["PYTHONUNBUFFERED"] = "1"
25
26 import samba.dcerpc.dcerpc as dcerpc
27 import samba.dcerpc.base as base
28 import samba.dcerpc.misc as misc
29 import samba.dcerpc.epmapper
30 import samba.dcerpc.mgmt
31 import samba.dcerpc.netlogon
32 import struct
33 from samba import gensec
34 from samba.tests.dcerpc.raw_testcase import RawDCERPCTest
35
36 global_ndr_print = False
37 global_hexdump = False
38
39 class TestDCERPC_BIND(RawDCERPCTest):
40
41     def setUp(self):
42         super(TestDCERPC_BIND, self).setUp()
43         self.do_ndr_print = global_ndr_print
44         self.do_hexdump = global_hexdump
45
46     def _test_no_auth_request_bind_pfc_flags(self, req_pfc_flags, rep_pfc_flags):
47         ndr32 = base.transfer_syntax_ndr()
48
49         tsf1_list = [ndr32]
50         ctx1 = dcerpc.ctx_list()
51         ctx1.context_id = 1
52         ctx1.num_transfer_syntaxes = len(tsf1_list)
53         ctx1.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
54         ctx1.transfer_syntaxes = tsf1_list
55
56         req = self.generate_bind(call_id=0, pfc_flags=req_pfc_flags, ctx_list=[ctx1])
57         self.send_pdu(req)
58         rep = self.recv_pdu()
59         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
60                         pfc_flags=rep_pfc_flags, auth_length=0)
61         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
62         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
63         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
64         self.assertEquals(rep.u.secondary_address_size, 4)
65         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
66         self.assertEquals(len(rep.u._pad1), 2)
67         # sometimes windows sends random bytes
68         # self.assertEquals(rep.u._pad1, '\0' * 2)
69         self.assertEquals(rep.u.num_results, 1)
70         self.assertEquals(rep.u.ctx_list[0].result,
71                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
72         self.assertEquals(rep.u.ctx_list[0].reason,
73                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
74         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
75         self.assertEquals(rep.u.auth_info, '\0' * 0)
76
77         # And now try a request
78         req = self.generate_request(call_id = 1,
79                                     context_id=ctx1.context_id,
80                                     opnum=0,
81                                     stub="")
82         self.send_pdu(req)
83         rep = self.recv_pdu()
84         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
85                         auth_length=0)
86         self.assertNotEquals(rep.u.alloc_hint, 0)
87         self.assertEquals(rep.u.context_id, req.u.context_id)
88         self.assertEquals(rep.u.cancel_count, 0)
89         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
90
91     def _test_no_auth_request_alter_pfc_flags(self, req_pfc_flags, rep_pfc_flags):
92         ndr32 = base.transfer_syntax_ndr()
93
94         tsf1_list = [ndr32]
95         ctx1 = dcerpc.ctx_list()
96         ctx1.context_id = 1
97         ctx1.num_transfer_syntaxes = len(tsf1_list)
98         ctx1.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
99         ctx1.transfer_syntaxes = tsf1_list
100
101         req = self.generate_bind(call_id=0, ctx_list=[ctx1])
102         self.send_pdu(req)
103         rep = self.recv_pdu()
104         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
105                         auth_length=0)
106         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
107         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
108         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
109         self.assertEquals(rep.u.secondary_address_size, 4)
110         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
111         self.assertEquals(len(rep.u._pad1), 2)
112         # sometimes windows sends random bytes
113         # self.assertEquals(rep.u._pad1, '\0' * 2)
114         self.assertEquals(rep.u.num_results, 1)
115         self.assertEquals(rep.u.ctx_list[0].result,
116                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
117         self.assertEquals(rep.u.ctx_list[0].reason,
118                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
119         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
120         self.assertEquals(rep.u.auth_info, '\0' * 0)
121
122         # And now try a alter context
123         req = self.generate_alter(call_id=0, pfc_flags=req_pfc_flags, ctx_list=[ctx1])
124         self.send_pdu(req)
125         rep = self.recv_pdu()
126         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id,
127                         pfc_flags=rep_pfc_flags, auth_length=0)
128         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
129         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
130         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
131         self.assertEquals(rep.u.secondary_address_size, 0)
132         self.assertEquals(rep.u.secondary_address, "")
133         self.assertEquals(len(rep.u._pad1), 2)
134         # sometimes windows sends random bytes
135         # self.assertEquals(rep.u._pad1, '\0' * 2)
136         self.assertEquals(rep.u.num_results, 1)
137         self.assertEquals(rep.u.ctx_list[0].result,
138                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
139         self.assertEquals(rep.u.ctx_list[0].reason,
140                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
141         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
142         self.assertEquals(rep.u.auth_info, '\0' * 0)
143
144         # And now try a request
145         req = self.generate_request(call_id = 1,
146                                     context_id=ctx1.context_id,
147                                     opnum=0,
148                                     stub="")
149         self.send_pdu(req)
150         rep = self.recv_pdu()
151         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
152                         auth_length=0)
153         self.assertNotEquals(rep.u.alloc_hint, 0)
154         self.assertEquals(rep.u.context_id, req.u.context_id)
155         self.assertEquals(rep.u.cancel_count, 0)
156         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
157
158     def test_no_auth_request(self):
159         return self._test_no_auth_request_bind_pfc_flags(
160                                         req_pfc_flags=0 |
161                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
162                                         dcerpc.DCERPC_PFC_FLAG_LAST,
163                                         rep_pfc_flags=0 |
164                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
165                                         dcerpc.DCERPC_PFC_FLAG_LAST)
166
167     def test_no_auth_request_bind_pfc_00(self):
168         return self._test_no_auth_request_bind_pfc_flags(
169                                         req_pfc_flags=0 |
170                                         0,
171                                         rep_pfc_flags=0 |
172                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
173                                         dcerpc.DCERPC_PFC_FLAG_LAST)
174
175     def test_no_auth_request_bind_pfc_FIRST(self):
176         return self._test_no_auth_request_bind_pfc_flags(
177                                         req_pfc_flags=0 |
178                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
179                                         0,
180                                         rep_pfc_flags=0 |
181                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
182                                         dcerpc.DCERPC_PFC_FLAG_LAST)
183
184     def test_no_auth_request_bind_pfc_LAST(self):
185         return self._test_no_auth_request_bind_pfc_flags(
186                                         req_pfc_flags=0 |
187                                         dcerpc.DCERPC_PFC_FLAG_LAST |
188                                         0,
189                                         rep_pfc_flags=0 |
190                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
191                                         dcerpc.DCERPC_PFC_FLAG_LAST)
192
193     # TODO: doesn't announce DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN
194     # without authentication
195     def _test_no_auth_request_bind_pfc_HDR_SIGNING(self):
196         return self._test_no_auth_request_bind_pfc_flags(
197                                         req_pfc_flags=0 |
198                                         dcerpc.DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN |
199                                         0,
200                                         rep_pfc_flags=0 |
201                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
202                                         dcerpc.DCERPC_PFC_FLAG_LAST |
203                                         dcerpc.DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN)
204
205     def test_no_auth_request_bind_pfc_08(self):
206         return self._test_no_auth_request_bind_pfc_flags(
207                                         req_pfc_flags=0 |
208                                         8 |
209                                         0,
210                                         rep_pfc_flags=0 |
211                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
212                                         dcerpc.DCERPC_PFC_FLAG_LAST)
213
214     def test_no_auth_request_bind_pfc_CONC_MPX(self):
215         return self._test_no_auth_request_bind_pfc_flags(
216                                         req_pfc_flags=0 |
217                                         dcerpc.DCERPC_PFC_FLAG_CONC_MPX |
218                                         0,
219                                         rep_pfc_flags=0 |
220                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
221                                         dcerpc.DCERPC_PFC_FLAG_LAST |
222                                         dcerpc.DCERPC_PFC_FLAG_CONC_MPX)
223
224     def test_no_auth_request_bind_pfc_DID_NOT_EXECUTE(self):
225         return self._test_no_auth_request_bind_pfc_flags(
226                                         req_pfc_flags=0 |
227                                         dcerpc.DCERPC_PFC_FLAG_DID_NOT_EXECUTE |
228                                         0,
229                                         rep_pfc_flags=0 |
230                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
231                                         dcerpc.DCERPC_PFC_FLAG_LAST)
232
233     def test_no_auth_request_bind_pfc_MAYBE(self):
234         return self._test_no_auth_request_bind_pfc_flags(
235                                         req_pfc_flags=0 |
236                                         dcerpc.DCERPC_PFC_FLAG_MAYBE |
237                                         0,
238                                         rep_pfc_flags=0 |
239                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
240                                         dcerpc.DCERPC_PFC_FLAG_LAST)
241
242     def test_no_auth_request_bind_pfc_OBJECT_UUID(self):
243         return self._test_no_auth_request_bind_pfc_flags(
244                                         req_pfc_flags=0 |
245                                         dcerpc.DCERPC_PFC_FLAG_OBJECT_UUID |
246                                         0,
247                                         rep_pfc_flags=0 |
248                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
249                                         dcerpc.DCERPC_PFC_FLAG_LAST)
250
251     # TODO: doesn't announce DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN
252     # without authentication
253     # TODO: doesn't announce DCERPC_PFC_FLAG_CONC_MPX
254     # by default
255     def _test_no_auth_request_bind_pfc_ff(self):
256         return self._test_no_auth_request_bind_pfc_flags(
257                                         req_pfc_flags=0 |
258                                         0xff |
259                                         0,
260                                         rep_pfc_flags=0 |
261                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
262                                         dcerpc.DCERPC_PFC_FLAG_LAST |
263                                         dcerpc.DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN |
264                                         dcerpc.DCERPC_PFC_FLAG_CONC_MPX)
265
266     def test_no_auth_request_alter_pfc_00(self):
267         return self._test_no_auth_request_alter_pfc_flags(
268                                         req_pfc_flags=0 |
269                                         0,
270                                         rep_pfc_flags=0 |
271                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
272                                         dcerpc.DCERPC_PFC_FLAG_LAST)
273
274     def test_no_auth_request_alter_pfc_FIRST(self):
275         return self._test_no_auth_request_alter_pfc_flags(
276                                         req_pfc_flags=0 |
277                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
278                                         0,
279                                         rep_pfc_flags=0 |
280                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
281                                         dcerpc.DCERPC_PFC_FLAG_LAST)
282
283     def test_no_auth_request_alter_pfc_LAST(self):
284         return self._test_no_auth_request_alter_pfc_flags(
285                                         req_pfc_flags=0 |
286                                         dcerpc.DCERPC_PFC_FLAG_LAST |
287                                         0,
288                                         rep_pfc_flags=0 |
289                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
290                                         dcerpc.DCERPC_PFC_FLAG_LAST)
291
292     # TODO: doesn't announce DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN
293     # without authentication
294     def _test_no_auth_request_alter_pfc_HDR_SIGNING(self):
295         return self._test_no_auth_request_alter_pfc_flags(
296                                         req_pfc_flags=0 |
297                                         dcerpc.DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN |
298                                         0,
299                                         rep_pfc_flags=0 |
300                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
301                                         dcerpc.DCERPC_PFC_FLAG_LAST |
302                                         dcerpc.DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN)
303
304     def test_no_auth_request_alter_pfc_08(self):
305         return self._test_no_auth_request_alter_pfc_flags(
306                                         req_pfc_flags=0 |
307                                         8 |
308                                         0,
309                                         rep_pfc_flags=0 |
310                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
311                                         dcerpc.DCERPC_PFC_FLAG_LAST)
312
313     def test_no_auth_request_alter_pfc_CONC_MPX(self):
314         return self._test_no_auth_request_alter_pfc_flags(
315                                         req_pfc_flags=0 |
316                                         dcerpc.DCERPC_PFC_FLAG_CONC_MPX |
317                                         0,
318                                         rep_pfc_flags=0 |
319                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
320                                         dcerpc.DCERPC_PFC_FLAG_LAST)
321
322     def test_no_auth_request_alter_pfc_DID_NOT_EXECUTE(self):
323         return self._test_no_auth_request_alter_pfc_flags(
324                                         req_pfc_flags=0 |
325                                         dcerpc.DCERPC_PFC_FLAG_DID_NOT_EXECUTE |
326                                         0,
327                                         rep_pfc_flags=0 |
328                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
329                                         dcerpc.DCERPC_PFC_FLAG_LAST)
330
331     def test_no_auth_request_alter_pfc_MAYBE(self):
332         return self._test_no_auth_request_alter_pfc_flags(
333                                         req_pfc_flags=0 |
334                                         dcerpc.DCERPC_PFC_FLAG_MAYBE |
335                                         0,
336                                         rep_pfc_flags=0 |
337                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
338                                         dcerpc.DCERPC_PFC_FLAG_LAST)
339
340     def test_no_auth_request_alter_pfc_OBJECT_UUID(self):
341         return self._test_no_auth_request_alter_pfc_flags(
342                                         req_pfc_flags=0 |
343                                         dcerpc.DCERPC_PFC_FLAG_OBJECT_UUID |
344                                         0,
345                                         rep_pfc_flags=0 |
346                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
347                                         dcerpc.DCERPC_PFC_FLAG_LAST)
348
349     # TODO: doesn't announce DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN
350     # without authentication
351     def _test_no_auth_request_alter_pfc_ff(self):
352         return self._test_no_auth_request_alter_pfc_flags(
353                                         req_pfc_flags=0 |
354                                         0xff |
355                                         0,
356                                         rep_pfc_flags=0 |
357                                         dcerpc.DCERPC_PFC_FLAG_FIRST |
358                                         dcerpc.DCERPC_PFC_FLAG_LAST |
359                                         dcerpc.DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN)
360
361     def test_no_auth_no_ctx(self):
362         # send an useless bind
363         req = self.generate_bind(call_id=0)
364         self.send_pdu(req)
365         rep = self.recv_pdu()
366         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_NAK, req.call_id,
367                         auth_length=0)
368         self.assertEquals(rep.u.reject_reason,
369                           dcerpc.DCERPC_BIND_NAK_REASON_NOT_SPECIFIED)
370         self.assertEquals(rep.u.num_versions, 1)
371         self.assertEquals(rep.u.versions[0].rpc_vers, req.rpc_vers)
372         self.assertEquals(rep.u.versions[0].rpc_vers_minor, req.rpc_vers_minor)
373         self.assertEquals(len(rep.u._pad), 3)
374         self.assertEquals(rep.u._pad, '\0' * 3)
375
376     def test_invalid_auth_noctx(self):
377         req = self.generate_bind(call_id=0)
378         req.auth_length = dcerpc.DCERPC_AUTH_TRAILER_LENGTH
379         self.send_pdu(req)
380         rep = self.recv_pdu()
381         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_NAK, req.call_id,
382                         auth_length=0)
383         self.assertEquals(rep.u.reject_reason,
384                           dcerpc.DCERPC_BIND_NAK_REASON_PROTOCOL_VERSION_NOT_SUPPORTED)
385         self.assertEquals(rep.u.num_versions, 1)
386         self.assertEquals(rep.u.versions[0].rpc_vers, req.rpc_vers)
387         self.assertEquals(rep.u.versions[0].rpc_vers_minor, req.rpc_vers_minor)
388         self.assertEquals(len(rep.u._pad), 3)
389         self.assertEquals(rep.u._pad, '\0' * 3)
390
391     def test_no_auth_valid_valid_request(self):
392         ndr32 = base.transfer_syntax_ndr()
393
394         tsf1_list = [ndr32]
395         ctx1 = dcerpc.ctx_list()
396         ctx1.context_id = 1
397         ctx1.num_transfer_syntaxes = len(tsf1_list)
398         ctx1.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
399         ctx1.transfer_syntaxes = tsf1_list
400
401         req = self.generate_bind(call_id=0, ctx_list=[ctx1])
402         self.send_pdu(req)
403         rep = self.recv_pdu()
404         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
405                         auth_length=0)
406         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
407         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
408         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
409         self.assertEquals(rep.u.secondary_address_size, 4)
410         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
411         self.assertEquals(len(rep.u._pad1), 2)
412         self.assertEquals(rep.u._pad1, '\0' * 2)
413         self.assertEquals(rep.u.num_results, 1)
414         self.assertEquals(rep.u.ctx_list[0].result,
415                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
416         self.assertEquals(rep.u.ctx_list[0].reason,
417                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
418         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
419         self.assertEquals(rep.u.auth_info, '\0' * 0)
420
421         # Send a bind again
422         tsf2_list = [ndr32]
423         ctx2 = dcerpc.ctx_list()
424         ctx2.context_id = 2
425         ctx2.num_transfer_syntaxes = len(tsf2_list)
426         ctx2.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
427         ctx2.transfer_syntaxes = tsf2_list
428
429         req = self.generate_bind(call_id=1, ctx_list=[ctx2])
430         self.send_pdu(req)
431         rep = self.recv_pdu()
432         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_NAK, req.call_id,
433                         auth_length=0)
434         self.assertEquals(rep.u.reject_reason,
435                           dcerpc.DCERPC_BIND_NAK_REASON_NOT_SPECIFIED)
436         self.assertEquals(rep.u.num_versions, 1)
437         self.assertEquals(rep.u.versions[0].rpc_vers, req.rpc_vers)
438         self.assertEquals(rep.u.versions[0].rpc_vers_minor, req.rpc_vers_minor)
439         self.assertEquals(len(rep.u._pad), 3)
440         self.assertEquals(rep.u._pad, '\0' * 3)
441
442         # wait for a disconnect
443         rep = self.recv_pdu()
444         self.assertIsNone(rep)
445         self.assertNotConnected()
446
447     def test_no_auth_invalid_valid_request(self):
448         # send an useless bind
449         req = self.generate_bind(call_id=0)
450         self.send_pdu(req)
451         rep = self.recv_pdu()
452         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_NAK, req.call_id,
453                         auth_length=0)
454         self.assertEquals(rep.u.reject_reason,
455                           dcerpc.DCERPC_BIND_NAK_REASON_NOT_SPECIFIED)
456         self.assertEquals(rep.u.num_versions, 1)
457         self.assertEquals(rep.u.versions[0].rpc_vers, req.rpc_vers)
458         self.assertEquals(rep.u.versions[0].rpc_vers_minor, req.rpc_vers_minor)
459         self.assertEquals(len(rep.u._pad), 3)
460         self.assertEquals(rep.u._pad, '\0' * 3)
461
462         # wait for a disconnect
463         rep = self.recv_pdu()
464         self.assertIsNone(rep)
465         self.assertNotConnected()
466
467     def test_alter_no_auth_no_ctx(self):
468         ndr32 = base.transfer_syntax_ndr()
469
470         tsf1_list = [ndr32]
471         ctx1 = dcerpc.ctx_list()
472         ctx1.context_id = 1
473         ctx1.num_transfer_syntaxes = len(tsf1_list)
474         ctx1.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
475         ctx1.transfer_syntaxes = tsf1_list
476
477         req = self.generate_bind(call_id=0, ctx_list=[ctx1])
478         self.send_pdu(req)
479         rep = self.recv_pdu()
480         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
481                         auth_length=0)
482         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
483         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
484         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
485         self.assertEquals(rep.u.secondary_address_size, 4)
486         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
487         self.assertEquals(len(rep.u._pad1), 2)
488         self.assertEquals(rep.u._pad1, '\0' * 2)
489         self.assertEquals(rep.u.num_results, 1)
490         self.assertEquals(rep.u.ctx_list[0].result,
491                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
492         self.assertEquals(rep.u.ctx_list[0].reason,
493                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
494         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
495         self.assertEquals(rep.u.auth_info, '\0' * 0)
496
497         # Send a alter
498         req = self.generate_alter(call_id=1, ctx_list=[])
499         self.send_pdu(req)
500         rep = self.recv_pdu()
501         self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
502                         pfc_flags=req.pfc_flags |
503                         dcerpc.DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
504                         auth_length=0)
505         self.assertNotEquals(rep.u.alloc_hint, 0)
506         self.assertEquals(rep.u.context_id, 0)
507         self.assertEquals(rep.u.cancel_count, 0)
508         self.assertEquals(rep.u.flags, 0)
509         self.assertEquals(rep.u.status, dcerpc.DCERPC_NCA_S_PROTO_ERROR)
510         self.assertEquals(rep.u.reserved, 0)
511         self.assertEquals(len(rep.u.error_and_verifier), 0)
512
513         # wait for a disconnect
514         rep = self.recv_pdu()
515         self.assertIsNone(rep)
516         self.assertNotConnected()
517
518     def test_no_auth_presentation_ctx_valid1(self):
519         ndr32 = base.transfer_syntax_ndr()
520
521         zero_syntax = misc.ndr_syntax_id()
522
523         tsf1_list = [zero_syntax, ndr32]
524         ctx1 = dcerpc.ctx_list()
525         ctx1.context_id = 1
526         ctx1.num_transfer_syntaxes = len(tsf1_list)
527         ctx1.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
528         ctx1.transfer_syntaxes = tsf1_list
529
530         req = self.generate_bind(call_id=0, ctx_list=[ctx1])
531         self.send_pdu(req)
532         rep = self.recv_pdu()
533         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
534                         auth_length=0)
535         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
536         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
537         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
538         self.assertEquals(rep.u.secondary_address_size, 4)
539         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
540         self.assertEquals(len(rep.u._pad1), 2)
541         self.assertEquals(rep.u._pad1, '\0' * 2)
542         self.assertEquals(rep.u.num_results, 1)
543         self.assertEquals(rep.u.ctx_list[0].result,
544                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
545         self.assertEquals(rep.u.ctx_list[0].reason,
546                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
547         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
548         self.assertEquals(rep.u.auth_info, '\0' * 0)
549
550         # Send a alter
551         req = self.generate_alter(call_id=1, ctx_list=[ctx1])
552         self.send_pdu(req)
553         rep = self.recv_pdu()
554         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id,
555                         auth_length=0)
556         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
557         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
558         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
559         self.assertEquals(rep.u.secondary_address_size, 0)
560         self.assertEquals(len(rep.u._pad1), 2)
561         #self.assertEquals(rep.u._pad1, '\0' * 2)
562         self.assertEquals(rep.u.num_results, 1)
563         self.assertEquals(rep.u.ctx_list[0].result,
564                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
565         self.assertEquals(rep.u.ctx_list[0].reason,
566                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
567         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
568         self.assertEquals(rep.u.auth_info, '\0' * 0)
569
570         req = self.generate_request(call_id = 2,
571                                     context_id=ctx1.context_id,
572                                     opnum=0xffff,
573                                     stub="")
574         self.send_pdu(req)
575         rep = self.recv_pdu()
576         self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
577                         pfc_flags=req.pfc_flags |
578                         dcerpc.DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
579                         auth_length=0)
580         self.assertNotEquals(rep.u.alloc_hint, 0)
581         self.assertEquals(rep.u.context_id, ctx1.context_id)
582         self.assertEquals(rep.u.cancel_count, 0)
583         self.assertEquals(rep.u.flags, 0)
584         self.assertEquals(rep.u.status, dcerpc.DCERPC_NCA_S_OP_RNG_ERROR)
585         self.assertEquals(rep.u.reserved, 0)
586         self.assertEquals(len(rep.u.error_and_verifier), 0)
587
588     def test_no_auth_presentation_ctx_invalid1(self):
589         ndr32 = base.transfer_syntax_ndr()
590
591         zero_syntax = misc.ndr_syntax_id()
592
593         tsf1_list = [ndr32]
594         ctx1 = dcerpc.ctx_list()
595         ctx1.context_id = 1
596         ctx1.num_transfer_syntaxes = len(tsf1_list)
597         ctx1.abstract_syntax = ndr32
598         ctx1.transfer_syntaxes = tsf1_list
599
600         req = self.generate_bind(call_id=0, ctx_list=[ctx1])
601         self.send_pdu(req)
602         rep = self.recv_pdu()
603         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
604                         auth_length=0)
605         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
606         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
607         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
608         self.assertEquals(rep.u.secondary_address_size, 4)
609         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
610         self.assertEquals(len(rep.u._pad1), 2)
611         self.assertEquals(rep.u._pad1, '\0' * 2)
612         self.assertEquals(rep.u.num_results, 1)
613         self.assertEquals(rep.u.ctx_list[0].result,
614                           dcerpc.DCERPC_BIND_ACK_RESULT_PROVIDER_REJECTION)
615         self.assertEquals(rep.u.ctx_list[0].reason,
616                           dcerpc.DCERPC_BIND_ACK_REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED)
617         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, zero_syntax)
618         self.assertEquals(rep.u.auth_info, '\0' * 0)
619
620         # Send a alter
621         req = self.generate_alter(call_id=1, ctx_list=[ctx1])
622         self.send_pdu(req)
623         rep = self.recv_pdu()
624         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id,
625                         auth_length=0)
626         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
627         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
628         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
629         self.assertEquals(rep.u.secondary_address_size, 0)
630         self.assertEquals(len(rep.u._pad1), 2)
631         #self.assertEquals(rep.u._pad1, '\0' * 2)
632         self.assertEquals(rep.u.num_results, 1)
633         self.assertEquals(rep.u.ctx_list[0].result,
634                           dcerpc.DCERPC_BIND_ACK_RESULT_PROVIDER_REJECTION)
635         self.assertEquals(rep.u.ctx_list[0].reason,
636                           dcerpc.DCERPC_BIND_ACK_REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED)
637         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, zero_syntax)
638         self.assertEquals(rep.u.auth_info, '\0' * 0)
639
640         req = self.generate_request(call_id = 2,
641                                     context_id=12345,
642                                     opnum=0,
643                                     stub="")
644         self.send_pdu(req)
645         rep = self.recv_pdu()
646         self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
647                         pfc_flags=req.pfc_flags |
648                         dcerpc.DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
649                         auth_length=0)
650         self.assertNotEquals(rep.u.alloc_hint, 0)
651         self.assertEquals(rep.u.context_id, 0)
652         self.assertEquals(rep.u.cancel_count, 0)
653         self.assertEquals(rep.u.flags, 0)
654         self.assertEquals(rep.u.status, dcerpc.DCERPC_NCA_S_UNKNOWN_IF)
655         self.assertEquals(rep.u.reserved, 0)
656         self.assertEquals(len(rep.u.error_and_verifier), 0)
657
658         # Send a alter again to prove the connection is still alive
659         req = self.generate_alter(call_id=3, ctx_list=[ctx1])
660         self.send_pdu(req)
661         rep = self.recv_pdu()
662         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id,
663                         auth_length=0)
664         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
665         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
666         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
667         self.assertEquals(rep.u.secondary_address_size, 0)
668         self.assertEquals(len(rep.u._pad1), 2)
669         #self.assertEquals(rep.u._pad1, '\0' * 2)
670         self.assertEquals(rep.u.num_results, 1)
671         self.assertEquals(rep.u.ctx_list[0].result,
672                           dcerpc.DCERPC_BIND_ACK_RESULT_PROVIDER_REJECTION)
673         self.assertEquals(rep.u.ctx_list[0].reason,
674                           dcerpc.DCERPC_BIND_ACK_REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED)
675         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, zero_syntax)
676         self.assertEquals(rep.u.auth_info, '\0' * 0)
677
678     def test_no_auth_presentation_ctx_invalid2(self):
679         ndr32 = base.transfer_syntax_ndr()
680
681         zero_syntax = misc.ndr_syntax_id()
682
683         tsf1a_list = []
684         ctx1a = dcerpc.ctx_list()
685         ctx1a.context_id = 1
686         ctx1a.num_transfer_syntaxes = len(tsf1a_list)
687         ctx1a.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
688         ctx1a.transfer_syntaxes = tsf1a_list
689
690         req = self.generate_bind(call_id=0, ctx_list=[ctx1a])
691         self.send_pdu(req)
692         rep = self.recv_pdu()
693         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_NAK, req.call_id,
694                         auth_length=0)
695         self.assertEquals(rep.u.reject_reason,
696                           dcerpc.DCERPC_BIND_NAK_REASON_NOT_SPECIFIED)
697         self.assertEquals(rep.u.num_versions, 1)
698         self.assertEquals(rep.u.versions[0].rpc_vers, req.rpc_vers)
699         self.assertEquals(rep.u.versions[0].rpc_vers_minor, req.rpc_vers_minor)
700         self.assertEquals(len(rep.u._pad), 3)
701         self.assertEquals(rep.u._pad, '\0' * 3)
702
703         # wait for a disconnect
704         rep = self.recv_pdu()
705         self.assertIsNone(rep)
706         self.assertNotConnected()
707
708     def test_no_auth_presentation_ctx_invalid3(self):
709         ndr32 = base.transfer_syntax_ndr()
710
711         zero_syntax = misc.ndr_syntax_id()
712
713         tsf1a_list = [zero_syntax, ndr32, ndr32, ndr32]
714         ctx1a = dcerpc.ctx_list()
715         ctx1a.context_id = 1
716         ctx1a.num_transfer_syntaxes = len(tsf1a_list)
717         ctx1a.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
718         ctx1a.transfer_syntaxes = tsf1a_list
719
720         req = self.generate_bind(call_id=0, ctx_list=[ctx1a])
721         self.send_pdu(req)
722         rep = self.recv_pdu()
723         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
724                         auth_length=0)
725         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
726         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
727         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
728         self.assertEquals(rep.u.secondary_address_size, 4)
729         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
730         self.assertEquals(len(rep.u._pad1), 2)
731         self.assertEquals(rep.u._pad1, '\0' * 2)
732         self.assertEquals(rep.u.num_results, 1)
733         self.assertEquals(rep.u.ctx_list[0].result,
734                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
735         self.assertEquals(rep.u.ctx_list[0].reason,
736                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
737         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
738         self.assertEquals(rep.u.auth_info, '\0' * 0)
739
740         tsf1b_list = []
741         ctx1b = dcerpc.ctx_list()
742         ctx1b.context_id = 1
743         ctx1b.num_transfer_syntaxes = len(tsf1b_list)
744         ctx1b.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
745         ctx1b.transfer_syntaxes = tsf1b_list
746
747         # Send a alter
748         req = self.generate_alter(call_id=1, ctx_list=[ctx1b])
749         self.send_pdu(req)
750         rep = self.recv_pdu()
751         self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
752                         pfc_flags=req.pfc_flags |
753                         dcerpc.DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
754                         auth_length=0)
755         self.assertNotEquals(rep.u.alloc_hint, 0)
756         self.assertEquals(rep.u.context_id, 0)
757         self.assertEquals(rep.u.cancel_count, 0)
758         self.assertEquals(rep.u.flags, 0)
759         self.assertEquals(rep.u.status, dcerpc.DCERPC_NCA_S_PROTO_ERROR)
760         self.assertEquals(rep.u.reserved, 0)
761         self.assertEquals(len(rep.u.error_and_verifier), 0)
762
763         # wait for a disconnect
764         rep = self.recv_pdu()
765         self.assertIsNone(rep)
766         self.assertNotConnected()
767
768     def test_no_auth_presentation_ctx_invalid4(self):
769         ndr32 = base.transfer_syntax_ndr()
770         ndr64 = base.transfer_syntax_ndr64()
771
772         zero_syntax = misc.ndr_syntax_id()
773
774         tsf1a_list = [zero_syntax, ndr32, ndr32, ndr32]
775         ctx1a = dcerpc.ctx_list()
776         ctx1a.context_id = 1
777         ctx1a.num_transfer_syntaxes = len(tsf1a_list)
778         ctx1a.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
779         ctx1a.transfer_syntaxes = tsf1a_list
780
781         req = self.generate_bind(call_id=0, ctx_list=[ctx1a])
782         self.send_pdu(req)
783         rep = self.recv_pdu()
784         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
785                         auth_length=0)
786         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
787         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
788         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
789         self.assertEquals(rep.u.secondary_address_size, 4)
790         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
791         self.assertEquals(len(rep.u._pad1), 2)
792         self.assertEquals(rep.u._pad1, '\0' * 2)
793         self.assertEquals(rep.u.num_results, 1)
794         self.assertEquals(rep.u.ctx_list[0].result,
795                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
796         self.assertEquals(rep.u.ctx_list[0].reason,
797                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
798         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
799         self.assertEquals(rep.u.auth_info, '\0' * 0)
800
801         # With a known but wrong syntax we get a protocol error
802         # see test_no_auth_presentation_ctx_valid2
803         tsf1b_list = [zero_syntax,samba.dcerpc.epmapper.abstract_syntax(),ndr64]
804         ctx1b = dcerpc.ctx_list()
805         ctx1b.context_id = 1
806         ctx1b.num_transfer_syntaxes = len(tsf1b_list)
807         ctx1b.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
808         ctx1b.transfer_syntaxes = tsf1b_list
809
810         # Send a alter
811         req = self.generate_alter(call_id=1, ctx_list=[ctx1b])
812         self.send_pdu(req)
813         rep = self.recv_pdu()
814         self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
815                         pfc_flags=req.pfc_flags |
816                         dcerpc.DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
817                         auth_length=0)
818         self.assertNotEquals(rep.u.alloc_hint, 0)
819         self.assertEquals(rep.u.context_id, 0)
820         self.assertEquals(rep.u.cancel_count, 0)
821         self.assertEquals(rep.u.flags, 0)
822         self.assertEquals(rep.u.status, dcerpc.DCERPC_NCA_S_PROTO_ERROR)
823         self.assertEquals(rep.u.reserved, 0)
824         self.assertEquals(len(rep.u.error_and_verifier), 0)
825
826         # wait for a disconnect
827         rep = self.recv_pdu()
828         self.assertIsNone(rep)
829         self.assertNotConnected()
830
831     def test_no_auth_presentation_ctx_valid2(self):
832         ndr32 = base.transfer_syntax_ndr()
833
834         zero_syntax = misc.ndr_syntax_id()
835
836         tsf1a_list = [zero_syntax, ndr32, ndr32, ndr32]
837         ctx1a = dcerpc.ctx_list()
838         ctx1a.context_id = 1
839         ctx1a.num_transfer_syntaxes = len(tsf1a_list)
840         ctx1a.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
841         ctx1a.transfer_syntaxes = tsf1a_list
842
843         req = self.generate_bind(call_id=0, ctx_list=[ctx1a])
844         self.send_pdu(req)
845         rep = self.recv_pdu()
846         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
847                         auth_length=0)
848         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
849         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
850         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
851         self.assertEquals(rep.u.secondary_address_size, 4)
852         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
853         self.assertEquals(len(rep.u._pad1), 2)
854         self.assertEquals(rep.u._pad1, '\0' * 2)
855         self.assertEquals(rep.u.num_results, 1)
856         self.assertEquals(rep.u.ctx_list[0].result,
857                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
858         self.assertEquals(rep.u.ctx_list[0].reason,
859                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
860         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
861         self.assertEquals(rep.u.auth_info, '\0' * 0)
862
863         # With a unknown but wrong syntaxes we get NO protocol error
864         # see test_no_auth_presentation_ctx_invalid4
865         tsf1b_list = [zero_syntax,samba.dcerpc.epmapper.abstract_syntax()]
866         ctx1b = dcerpc.ctx_list()
867         ctx1b.context_id = 1
868         ctx1b.num_transfer_syntaxes = len(tsf1b_list)
869         ctx1b.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
870         ctx1b.transfer_syntaxes = tsf1b_list
871
872         # Send a alter
873         req = self.generate_alter(call_id=1, ctx_list=[ctx1b])
874         self.send_pdu(req)
875         rep = self.recv_pdu()
876         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id,
877                         auth_length=0)
878         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
879         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
880         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
881         self.assertEquals(rep.u.secondary_address_size, 0)
882         self.assertEquals(len(rep.u._pad1), 2)
883         #self.assertEquals(rep.u._pad1, '\0' * 2)
884         self.assertEquals(rep.u.num_results, 1)
885         self.assertEquals(rep.u.ctx_list[0].result,
886                           dcerpc.DCERPC_BIND_ACK_RESULT_PROVIDER_REJECTION)
887         self.assertEquals(rep.u.ctx_list[0].reason,
888                           dcerpc.DCERPC_BIND_ACK_REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED)
889         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, zero_syntax)
890         self.assertEquals(rep.u.auth_info, '\0' * 0)
891
892         req = self.generate_request(call_id = 2,
893                                     context_id=ctx1a.context_id,
894                                     opnum=0xffff,
895                                     stub="")
896         self.send_pdu(req)
897         rep = self.recv_pdu()
898         self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
899                         pfc_flags=req.pfc_flags |
900                         dcerpc.DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
901                         auth_length=0)
902         self.assertNotEquals(rep.u.alloc_hint, 0)
903         self.assertEquals(rep.u.context_id, ctx1a.context_id)
904         self.assertEquals(rep.u.cancel_count, 0)
905         self.assertEquals(rep.u.flags, 0)
906         self.assertEquals(rep.u.status, dcerpc.DCERPC_NCA_S_OP_RNG_ERROR)
907         self.assertEquals(rep.u.reserved, 0)
908         self.assertEquals(len(rep.u.error_and_verifier), 0)
909
910     def test_no_auth_presentation_ctx_no_ndr64(self):
911         ndr32 = base.transfer_syntax_ndr()
912         zero_syntax = misc.ndr_syntax_id()
913
914         tsfZ_list = [zero_syntax]
915         ctxZ = dcerpc.ctx_list()
916         ctxZ.context_id = 54321
917         ctxZ.num_transfer_syntaxes = len(tsfZ_list)
918         ctxZ.abstract_syntax = zero_syntax
919         ctxZ.transfer_syntaxes = tsfZ_list
920
921         req = self.generate_bind(call_id=0, ctx_list=[ctxZ])
922         self.send_pdu(req)
923         rep = self.recv_pdu()
924         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
925                         auth_length=0)
926         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
927         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
928         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
929         self.assertEquals(rep.u.secondary_address_size, 4)
930         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
931         self.assertEquals(len(rep.u._pad1), 2)
932         self.assertEquals(rep.u._pad1, '\0' * 2)
933         self.assertEquals(rep.u.num_results, 1)
934         self.assertEquals(rep.u.ctx_list[0].result,
935                           dcerpc.DCERPC_BIND_ACK_RESULT_PROVIDER_REJECTION)
936         self.assertEquals(rep.u.ctx_list[0].reason,
937                           dcerpc.DCERPC_BIND_ACK_REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED)
938         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, zero_syntax)
939         self.assertEquals(rep.u.auth_info, '\0' * 0)
940
941         tsf0_list = [ndr32]
942         ctx0 = dcerpc.ctx_list()
943         ctx0.context_id = 0
944         ctx0.num_transfer_syntaxes = len(tsf0_list)
945         ctx0.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
946         ctx0.transfer_syntaxes = tsf0_list
947
948         req = self.generate_alter(call_id=0, ctx_list=[ctx0])
949         self.send_pdu(req)
950         rep = self.recv_pdu()
951         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id,
952                         auth_length=0)
953         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
954         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
955         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
956         self.assertEquals(rep.u.secondary_address_size, 0)
957         self.assertEquals(len(rep.u._pad1), 2)
958         #self.assertEquals(rep.u._pad1, '\0' * 2)
959         self.assertEquals(rep.u.num_results, 1)
960         self.assertEquals(rep.u.ctx_list[0].result,
961                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
962         self.assertEquals(rep.u.ctx_list[0].reason,
963                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
964         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
965         self.assertEquals(rep.u.auth_info, '\0' * 0)
966
967         req = self.generate_request(call_id = 1,
968                                     context_id=ctx0.context_id,
969                                     opnum=0,
970                                     stub="")
971         self.send_pdu(req)
972         rep = self.recv_pdu()
973         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
974                         auth_length=0)
975         self.assertNotEquals(rep.u.alloc_hint, 0)
976         self.assertEquals(rep.u.context_id, req.u.context_id)
977         self.assertEquals(rep.u.cancel_count, 0)
978         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
979
980         tsf1_list = [zero_syntax,ndr32]
981         ctx1 = dcerpc.ctx_list()
982         ctx1.context_id = 1
983         ctx1.num_transfer_syntaxes = len(tsf1_list)
984         ctx1.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
985         ctx1.transfer_syntaxes = tsf1_list
986
987         req = self.generate_alter(call_id=1, ctx_list=[ctx1])
988         self.send_pdu(req)
989         rep = self.recv_pdu()
990         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id,
991                         auth_length=0)
992         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
993         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
994         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
995         self.assertEquals(rep.u.secondary_address_size, 0)
996         self.assertEquals(len(rep.u._pad1), 2)
997         #self.assertEquals(rep.u._pad1, '\0' * 2)
998         self.assertEquals(rep.u.num_results, 1)
999         self.assertEquals(rep.u.ctx_list[0].result,
1000                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
1001         self.assertEquals(rep.u.ctx_list[0].reason,
1002                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
1003         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
1004         self.assertEquals(rep.u.auth_info, '\0' * 0)
1005
1006         req = self.generate_request(call_id = 1,
1007                                     context_id=ctx1.context_id,
1008                                     opnum=0,
1009                                     stub="")
1010         self.send_pdu(req)
1011         rep = self.recv_pdu()
1012         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1013                         auth_length=0)
1014         self.assertNotEquals(rep.u.alloc_hint, 0)
1015         self.assertEquals(rep.u.context_id, req.u.context_id)
1016         self.assertEquals(rep.u.cancel_count, 0)
1017         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1018
1019         tsf2_list = [ndr32,ndr32]
1020         ctx2 = dcerpc.ctx_list()
1021         ctx2.context_id = 2
1022         ctx2.num_transfer_syntaxes = len(tsf2_list)
1023         ctx2.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
1024         ctx2.transfer_syntaxes = tsf2_list
1025
1026         req = self.generate_alter(call_id=2, ctx_list=[ctx2])
1027         self.send_pdu(req)
1028         rep = self.recv_pdu()
1029         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id,
1030                         auth_length=0)
1031         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
1032         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
1033         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
1034         self.assertEquals(rep.u.secondary_address_size, 0)
1035         self.assertEquals(len(rep.u._pad1), 2)
1036         #self.assertEquals(rep.u._pad1, '\0' * 2)
1037         self.assertEquals(rep.u.num_results, 1)
1038         self.assertEquals(rep.u.ctx_list[0].result,
1039                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
1040         self.assertEquals(rep.u.ctx_list[0].reason,
1041                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
1042         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
1043         self.assertEquals(rep.u.auth_info, '\0' * 0)
1044
1045         req = self.generate_request(call_id = 1,
1046                                     context_id=ctx2.context_id,
1047                                     opnum=0,
1048                                     stub="")
1049         self.send_pdu(req)
1050         rep = self.recv_pdu()
1051         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1052                         auth_length=0)
1053         self.assertNotEquals(rep.u.alloc_hint, 0)
1054         self.assertEquals(rep.u.context_id, req.u.context_id)
1055         self.assertEquals(rep.u.cancel_count, 0)
1056         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1057
1058         tsf3_list = [ndr32]
1059         ctx3 = dcerpc.ctx_list()
1060         ctx3.context_id = 3
1061         ctx3.num_transfer_syntaxes = len(tsf3_list)
1062         ctx3.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
1063         ctx3.transfer_syntaxes = tsf3_list
1064
1065         tsf4_list = [ndr32]
1066         ctx4 = dcerpc.ctx_list()
1067         ctx4.context_id = 4
1068         ctx4.num_transfer_syntaxes = len(tsf4_list)
1069         ctx4.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
1070         ctx4.transfer_syntaxes = tsf4_list
1071
1072         req = self.generate_alter(call_id=34, ctx_list=[ctx3,ctx4])
1073         self.send_pdu(req)
1074         rep = self.recv_pdu()
1075         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id,
1076                         auth_length=0)
1077         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
1078         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
1079         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
1080         self.assertEquals(rep.u.secondary_address_size, 0)
1081         self.assertEquals(len(rep.u._pad1), 2)
1082         #self.assertEquals(rep.u._pad1, '\0' * 2)
1083         self.assertEquals(rep.u.num_results, 2)
1084         self.assertEquals(rep.u.ctx_list[0].result,
1085                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
1086         self.assertEquals(rep.u.ctx_list[0].reason,
1087                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
1088         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
1089         self.assertEquals(rep.u.ctx_list[1].result,
1090                           dcerpc.DCERPC_BIND_ACK_RESULT_PROVIDER_REJECTION)
1091         self.assertEquals(rep.u.ctx_list[1].reason,
1092                           dcerpc.DCERPC_BIND_ACK_REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED)
1093         self.assertNDRSyntaxEquals(rep.u.ctx_list[1].syntax, zero_syntax)
1094         self.assertEquals(rep.u.auth_info, '\0' * 0)
1095
1096         req = self.generate_request(call_id = 1,
1097                                     context_id=ctx3.context_id,
1098                                     opnum=0,
1099                                     stub="")
1100         self.send_pdu(req)
1101         rep = self.recv_pdu()
1102         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1103                         auth_length=0)
1104         self.assertNotEquals(rep.u.alloc_hint, 0)
1105         self.assertEquals(rep.u.context_id, req.u.context_id)
1106         self.assertEquals(rep.u.cancel_count, 0)
1107         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1108
1109         req = self.generate_alter(call_id=43, ctx_list=[ctx4,ctx3])
1110         self.send_pdu(req)
1111         rep = self.recv_pdu()
1112         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id,
1113                         auth_length=0)
1114         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
1115         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
1116         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
1117         self.assertEquals(rep.u.secondary_address_size, 0)
1118         self.assertEquals(len(rep.u._pad1), 2)
1119         #self.assertEquals(rep.u._pad1, '\0' * 2)
1120         self.assertEquals(rep.u.num_results, 2)
1121         self.assertEquals(rep.u.ctx_list[0].result,
1122                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
1123         self.assertEquals(rep.u.ctx_list[0].reason,
1124                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
1125         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
1126         self.assertEquals(rep.u.ctx_list[1].result,
1127                           dcerpc.DCERPC_BIND_ACK_RESULT_PROVIDER_REJECTION)
1128         self.assertEquals(rep.u.ctx_list[1].reason,
1129                           dcerpc.DCERPC_BIND_ACK_REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED)
1130         self.assertNDRSyntaxEquals(rep.u.ctx_list[1].syntax, zero_syntax)
1131         self.assertEquals(rep.u.auth_info, '\0' * 0)
1132
1133         req = self.generate_request(call_id = 1,
1134                                     context_id=ctx4.context_id,
1135                                     opnum=0,
1136                                     stub="")
1137         self.send_pdu(req)
1138         rep = self.recv_pdu()
1139         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1140                         auth_length=0)
1141         self.assertNotEquals(rep.u.alloc_hint, 0)
1142         self.assertEquals(rep.u.context_id, req.u.context_id)
1143         self.assertEquals(rep.u.cancel_count, 0)
1144         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1145
1146         req = self.generate_request(call_id = 1,
1147                                     context_id=ctx3.context_id,
1148                                     opnum=0,
1149                                     stub="")
1150         self.send_pdu(req)
1151         rep = self.recv_pdu()
1152         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1153                         auth_length=0)
1154         self.assertNotEquals(rep.u.alloc_hint, 0)
1155         self.assertEquals(rep.u.context_id, req.u.context_id)
1156         self.assertEquals(rep.u.cancel_count, 0)
1157         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1158
1159         req = self.generate_alter(call_id=44, ctx_list=[ctx4,ctx4])
1160         self.send_pdu(req)
1161         rep = self.recv_pdu()
1162         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id,
1163                         auth_length=0)
1164         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
1165         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
1166         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
1167         self.assertEquals(rep.u.secondary_address_size, 0)
1168         self.assertEquals(len(rep.u._pad1), 2)
1169         #self.assertEquals(rep.u._pad1, '\0' * 2)
1170         self.assertEquals(rep.u.num_results, 2)
1171         self.assertEquals(rep.u.ctx_list[0].result,
1172                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
1173         self.assertEquals(rep.u.ctx_list[0].reason,
1174                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
1175         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
1176         self.assertEquals(rep.u.ctx_list[1].result,
1177                           dcerpc.DCERPC_BIND_ACK_RESULT_PROVIDER_REJECTION)
1178         self.assertEquals(rep.u.ctx_list[1].reason,
1179                           dcerpc.DCERPC_BIND_ACK_REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED)
1180         self.assertNDRSyntaxEquals(rep.u.ctx_list[1].syntax, zero_syntax)
1181         self.assertEquals(rep.u.auth_info, '\0' * 0)
1182
1183         req = self.generate_request(call_id = 1,
1184                                     context_id=ctx4.context_id,
1185                                     opnum=0,
1186                                     stub="")
1187         self.send_pdu(req)
1188         rep = self.recv_pdu()
1189         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1190                         auth_length=0)
1191         self.assertNotEquals(rep.u.alloc_hint, 0)
1192         self.assertEquals(rep.u.context_id, req.u.context_id)
1193         self.assertEquals(rep.u.cancel_count, 0)
1194         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1195
1196         req = self.generate_request(call_id = 1,
1197                                     context_id=ctx3.context_id,
1198                                     opnum=0,
1199                                     stub="")
1200         self.send_pdu(req)
1201         rep = self.recv_pdu()
1202         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1203                         auth_length=0)
1204         self.assertNotEquals(rep.u.alloc_hint, 0)
1205         self.assertEquals(rep.u.context_id, req.u.context_id)
1206         self.assertEquals(rep.u.cancel_count, 0)
1207         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1208
1209         tsf5mgmt_list = [ndr32]
1210         ctx5mgmt = dcerpc.ctx_list()
1211         ctx5mgmt.context_id = 5
1212         ctx5mgmt.num_transfer_syntaxes = len(tsf5mgmt_list)
1213         ctx5mgmt.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
1214         ctx5mgmt.transfer_syntaxes = tsf5mgmt_list
1215
1216         tsf5epm_list = [ndr32]
1217         ctx5epm = dcerpc.ctx_list()
1218         ctx5epm.context_id = 5
1219         ctx5epm.num_transfer_syntaxes = len(tsf5epm_list)
1220         ctx5epm.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
1221         ctx5epm.transfer_syntaxes = tsf5epm_list
1222
1223         req = self.generate_alter(call_id=55, ctx_list=[ctx5mgmt,ctx5epm])
1224         self.send_pdu(req)
1225         rep = self.recv_pdu()
1226         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id,
1227                         auth_length=0)
1228         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
1229         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
1230         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
1231         self.assertEquals(rep.u.secondary_address_size, 0)
1232         self.assertEquals(len(rep.u._pad1), 2)
1233         #self.assertEquals(rep.u._pad1, '\0' * 2)
1234         self.assertEquals(rep.u.num_results, 2)
1235         self.assertEquals(rep.u.ctx_list[0].result,
1236                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
1237         self.assertEquals(rep.u.ctx_list[0].reason,
1238                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
1239         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
1240         self.assertEquals(rep.u.ctx_list[1].result,
1241                           dcerpc.DCERPC_BIND_ACK_RESULT_PROVIDER_REJECTION)
1242         self.assertEquals(rep.u.ctx_list[1].reason,
1243                           dcerpc.DCERPC_BIND_ACK_REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED)
1244         self.assertNDRSyntaxEquals(rep.u.ctx_list[1].syntax, zero_syntax)
1245         self.assertEquals(rep.u.auth_info, '\0' * 0)
1246
1247         req = self.generate_request(call_id = 1,
1248                                     context_id=ctx5mgmt.context_id,
1249                                     opnum=0,
1250                                     stub="")
1251         self.send_pdu(req)
1252         rep = self.recv_pdu()
1253         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1254                         auth_length=0)
1255         self.assertNotEquals(rep.u.alloc_hint, 0)
1256         self.assertEquals(rep.u.context_id, req.u.context_id)
1257         self.assertEquals(rep.u.cancel_count, 0)
1258         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1259
1260         req = self.generate_alter(call_id=55, ctx_list=[ctx5mgmt,ctx5epm])
1261         self.send_pdu(req)
1262         rep = self.recv_pdu()
1263         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id,
1264                         auth_length=0)
1265         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
1266         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
1267         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
1268         self.assertEquals(rep.u.secondary_address_size, 0)
1269         self.assertEquals(len(rep.u._pad1), 2)
1270         #self.assertEquals(rep.u._pad1, '\0' * 2)
1271         self.assertEquals(rep.u.num_results, 2)
1272         self.assertEquals(rep.u.ctx_list[0].result,
1273                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
1274         self.assertEquals(rep.u.ctx_list[0].reason,
1275                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
1276         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
1277         self.assertEquals(rep.u.ctx_list[1].result,
1278                           dcerpc.DCERPC_BIND_ACK_RESULT_PROVIDER_REJECTION)
1279         self.assertEquals(rep.u.ctx_list[1].reason,
1280                           dcerpc.DCERPC_BIND_ACK_REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED)
1281         self.assertNDRSyntaxEquals(rep.u.ctx_list[1].syntax, zero_syntax)
1282         self.assertEquals(rep.u.auth_info, '\0' * 0)
1283
1284         req = self.generate_request(call_id = 1,
1285                                     context_id=ctx5mgmt.context_id,
1286                                     opnum=0,
1287                                     stub="")
1288         self.send_pdu(req)
1289         rep = self.recv_pdu()
1290         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1291                         auth_length=0)
1292         self.assertNotEquals(rep.u.alloc_hint, 0)
1293         self.assertEquals(rep.u.context_id, req.u.context_id)
1294         self.assertEquals(rep.u.cancel_count, 0)
1295         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1296
1297     def test_no_auth_bind_time_none_simple(self):
1298         features = 0
1299         btf = base.bind_time_features_syntax(features)
1300
1301         zero_syntax = misc.ndr_syntax_id()
1302
1303         tsf1_list = [btf]
1304         ctx1 = dcerpc.ctx_list()
1305         ctx1.context_id = 1
1306         ctx1.num_transfer_syntaxes = len(tsf1_list)
1307         ctx1.abstract_syntax = zero_syntax
1308         ctx1.transfer_syntaxes = tsf1_list
1309
1310         req = self.generate_bind(call_id=0, ctx_list=[ctx1])
1311         self.send_pdu(req)
1312         rep = self.recv_pdu()
1313         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
1314                         auth_length=0)
1315         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
1316         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
1317         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
1318         self.assertEquals(rep.u.secondary_address_size, 4)
1319         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
1320         self.assertEquals(len(rep.u._pad1), 2)
1321         self.assertEquals(rep.u._pad1, '\0' * 2)
1322         self.assertEquals(rep.u.num_results, 1)
1323         self.assertEquals(rep.u.ctx_list[0].result,
1324                           dcerpc.DCERPC_BIND_ACK_RESULT_NEGOTIATE_ACK)
1325         self.assertEquals(rep.u.ctx_list[0].reason, features)
1326         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, zero_syntax)
1327         self.assertEquals(rep.u.auth_info, '\0' * 0)
1328
1329     def test_no_auth_bind_time_none_ignore_additional(self):
1330         features1 = 0
1331         btf1 = base.bind_time_features_syntax(features1)
1332
1333         features2 = dcerpc.DCERPC_BIND_TIME_KEEP_CONNECTION_ON_ORPHAN
1334         features2 |= dcerpc.DCERPC_BIND_TIME_SECURITY_CONTEXT_MULTIPLEXING
1335         btf2 = base.bind_time_features_syntax(features2)
1336
1337         zero_syntax = misc.ndr_syntax_id()
1338         ndr64 = base.transfer_syntax_ndr64()
1339
1340         tsf1_list = [btf1,btf2,zero_syntax]
1341         ctx1 = dcerpc.ctx_list()
1342         ctx1.context_id = 1
1343         ctx1.num_transfer_syntaxes = len(tsf1_list)
1344         ctx1.abstract_syntax = ndr64
1345         ctx1.transfer_syntaxes = tsf1_list
1346
1347         req = self.generate_bind(call_id=0, ctx_list=[ctx1])
1348         self.send_pdu(req)
1349         rep = self.recv_pdu()
1350         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
1351                         auth_length=0)
1352         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
1353         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
1354         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
1355         self.assertEquals(rep.u.secondary_address_size, 4)
1356         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
1357         self.assertEquals(len(rep.u._pad1), 2)
1358         self.assertEquals(rep.u._pad1, '\0' * 2)
1359         self.assertEquals(rep.u.num_results, 1)
1360         self.assertEquals(rep.u.ctx_list[0].result,
1361                           dcerpc.DCERPC_BIND_ACK_RESULT_NEGOTIATE_ACK)
1362         self.assertEquals(rep.u.ctx_list[0].reason, features1)
1363         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, zero_syntax)
1364         self.assertEquals(rep.u.auth_info, '\0' * 0)
1365
1366     def test_no_auth_bind_time_only_first(self):
1367         features1 = dcerpc.DCERPC_BIND_TIME_KEEP_CONNECTION_ON_ORPHAN
1368         btf1 = base.bind_time_features_syntax(features1)
1369
1370         features2 = dcerpc.DCERPC_BIND_TIME_SECURITY_CONTEXT_MULTIPLEXING
1371         btf2 = base.bind_time_features_syntax(features2)
1372
1373         zero_syntax = misc.ndr_syntax_id()
1374
1375         tsf1_list = [zero_syntax,btf1,btf2,zero_syntax]
1376         ctx1 = dcerpc.ctx_list()
1377         ctx1.context_id = 1
1378         ctx1.num_transfer_syntaxes = len(tsf1_list)
1379         ctx1.abstract_syntax = zero_syntax
1380         ctx1.transfer_syntaxes = tsf1_list
1381
1382         req = self.generate_bind(call_id=0, ctx_list=[ctx1])
1383         self.send_pdu(req)
1384         rep = self.recv_pdu()
1385         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
1386                         auth_length=0)
1387         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
1388         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
1389         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
1390         self.assertEquals(rep.u.secondary_address_size, 4)
1391         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
1392         self.assertEquals(len(rep.u._pad1), 2)
1393         self.assertEquals(rep.u._pad1, '\0' * 2)
1394         self.assertEquals(rep.u.num_results, 1)
1395         self.assertEquals(rep.u.ctx_list[0].result,
1396                           dcerpc.DCERPC_BIND_ACK_RESULT_PROVIDER_REJECTION)
1397         self.assertEquals(rep.u.ctx_list[0].reason,
1398                           dcerpc.DCERPC_BIND_ACK_REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED)
1399         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, zero_syntax)
1400         self.assertEquals(rep.u.auth_info, '\0' * 0)
1401
1402     def test_no_auth_bind_time_twice(self):
1403         features1 = dcerpc.DCERPC_BIND_TIME_KEEP_CONNECTION_ON_ORPHAN
1404         btf1 = base.bind_time_features_syntax(features1)
1405
1406         features2 = dcerpc.DCERPC_BIND_TIME_SECURITY_CONTEXT_MULTIPLEXING
1407         btf2 = base.bind_time_features_syntax(features2)
1408
1409         zero_syntax = misc.ndr_syntax_id()
1410
1411         tsf1_list = [btf1]
1412         ctx1 = dcerpc.ctx_list()
1413         ctx1.context_id = 1
1414         ctx1.num_transfer_syntaxes = len(tsf1_list)
1415         ctx1.abstract_syntax = zero_syntax
1416         ctx1.transfer_syntaxes = tsf1_list
1417
1418         tsf2_list = [btf2]
1419         ctx2 = dcerpc.ctx_list()
1420         ctx2.context_id = 2
1421         ctx2.num_transfer_syntaxes = len(tsf2_list)
1422         ctx2.abstract_syntax = zero_syntax
1423         ctx2.transfer_syntaxes = tsf2_list
1424
1425         req = self.generate_bind(call_id=0, ctx_list=[ctx1,ctx2])
1426         self.send_pdu(req)
1427         rep = self.recv_pdu()
1428         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_NAK, req.call_id,
1429                         auth_length=0)
1430         self.assertEquals(rep.u.reject_reason,
1431                           dcerpc.DCERPC_BIND_NAK_REASON_NOT_SPECIFIED)
1432         self.assertEquals(rep.u.num_versions, 1)
1433         self.assertEquals(rep.u.versions[0].rpc_vers, req.rpc_vers)
1434         self.assertEquals(rep.u.versions[0].rpc_vers_minor, req.rpc_vers_minor)
1435         self.assertEquals(len(rep.u._pad), 3)
1436         self.assertEquals(rep.u._pad, '\0' * 3)
1437
1438         # wait for a disconnect
1439         rep = self.recv_pdu()
1440         self.assertIsNone(rep)
1441         self.assertNotConnected()
1442
1443     def test_no_auth_bind_time_keep_on_orphan_simple(self):
1444         features = dcerpc.DCERPC_BIND_TIME_KEEP_CONNECTION_ON_ORPHAN
1445         btf = base.bind_time_features_syntax(features)
1446
1447         zero_syntax = misc.ndr_syntax_id()
1448
1449         tsf1_list = [btf]
1450         ctx1 = dcerpc.ctx_list()
1451         ctx1.context_id = 1
1452         ctx1.num_transfer_syntaxes = len(tsf1_list)
1453         ctx1.abstract_syntax = zero_syntax
1454         ctx1.transfer_syntaxes = tsf1_list
1455
1456         req = self.generate_bind(call_id=0, ctx_list=[ctx1])
1457         self.send_pdu(req)
1458         rep = self.recv_pdu()
1459         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
1460                         auth_length=0)
1461         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
1462         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
1463         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
1464         self.assertEquals(rep.u.secondary_address_size, 4)
1465         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
1466         self.assertEquals(len(rep.u._pad1), 2)
1467         self.assertEquals(rep.u._pad1, '\0' * 2)
1468         self.assertEquals(rep.u.num_results, 1)
1469         self.assertEquals(rep.u.ctx_list[0].result,
1470                           dcerpc.DCERPC_BIND_ACK_RESULT_NEGOTIATE_ACK)
1471         self.assertEquals(rep.u.ctx_list[0].reason, features)
1472         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, zero_syntax)
1473         self.assertEquals(rep.u.auth_info, '\0' * 0)
1474
1475     def test_no_auth_bind_time_keep_on_orphan_ignore_additional(self):
1476         features1 = dcerpc.DCERPC_BIND_TIME_KEEP_CONNECTION_ON_ORPHAN
1477         btf1 = base.bind_time_features_syntax(features1)
1478
1479         features2 = dcerpc.DCERPC_BIND_TIME_SECURITY_CONTEXT_MULTIPLEXING
1480         btf2 = base.bind_time_features_syntax(features2)
1481
1482         zero_syntax = misc.ndr_syntax_id()
1483         ndr64 = base.transfer_syntax_ndr64()
1484
1485         tsf1_list = [btf1,btf2,zero_syntax]
1486         ctx1 = dcerpc.ctx_list()
1487         ctx1.context_id = 1
1488         ctx1.num_transfer_syntaxes = len(tsf1_list)
1489         ctx1.abstract_syntax = ndr64
1490         ctx1.transfer_syntaxes = tsf1_list
1491
1492         req = self.generate_bind(call_id=0, ctx_list=[ctx1])
1493         self.send_pdu(req)
1494         rep = self.recv_pdu()
1495         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
1496                         auth_length=0)
1497         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
1498         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
1499         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
1500         self.assertEquals(rep.u.secondary_address_size, 4)
1501         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
1502         self.assertEquals(len(rep.u._pad1), 2)
1503         self.assertEquals(rep.u._pad1, '\0' * 2)
1504         self.assertEquals(rep.u.num_results, 1)
1505         self.assertEquals(rep.u.ctx_list[0].result,
1506                           dcerpc.DCERPC_BIND_ACK_RESULT_NEGOTIATE_ACK)
1507         self.assertEquals(rep.u.ctx_list[0].reason, features1)
1508         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, zero_syntax)
1509         self.assertEquals(rep.u.auth_info, '\0' * 0)
1510
1511     def _test_auth_type_level_bind_nak(self, auth_type, auth_level, creds=None,
1512                                        reason=dcerpc.DCERPC_BIND_NAK_REASON_INVALID_AUTH_TYPE):
1513         ndr32 = base.transfer_syntax_ndr()
1514
1515         tsf1_list = [ndr32]
1516         ctx1 = dcerpc.ctx_list()
1517         ctx1.context_id = 1
1518         ctx1.num_transfer_syntaxes = len(tsf1_list)
1519         ctx1.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
1520         ctx1.transfer_syntaxes = tsf1_list
1521         ctx_list = [ctx1]
1522
1523         auth_context_id = 0
1524
1525         if creds is not None:
1526             # We always start with DCERPC_AUTH_LEVEL_INTEGRITY
1527             auth_context = self.get_auth_context_creds(creds,
1528                                                        auth_type=auth_type,
1529                                                        auth_level=auth_level,
1530                                                        auth_context_id=auth_context_id,
1531                                                        g_auth_level=dcerpc.DCERPC_AUTH_LEVEL_INTEGRITY)
1532             from_server = ""
1533             (finished, to_server) = auth_context["gensec"].update(from_server)
1534             self.assertFalse(finished)
1535
1536             auth_info = self.generate_auth(auth_type=auth_context["auth_type"],
1537                                            auth_level=auth_context["auth_level"],
1538                                            auth_context_id=auth_context["auth_context_id"],
1539                                            auth_blob=to_server)
1540         else:
1541             to_server = "none"
1542             auth_info = self.generate_auth(auth_type=auth_type,
1543                                            auth_level=auth_level,
1544                                            auth_context_id=auth_context_id,
1545                                            auth_blob=to_server)
1546
1547         req = self.generate_bind(call_id=0,
1548                                  ctx_list=ctx_list,
1549                                  auth_info=auth_info)
1550         self.send_pdu(req)
1551         rep = self.recv_pdu()
1552         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_NAK, req.call_id,
1553                         auth_length=0)
1554         self.assertEquals(rep.u.reject_reason, reason)
1555         self.assertEquals(rep.u.num_versions, 1)
1556         self.assertEquals(rep.u.versions[0].rpc_vers, req.rpc_vers)
1557         self.assertEquals(rep.u.versions[0].rpc_vers_minor, req.rpc_vers_minor)
1558         self.assertEquals(len(rep.u._pad), 3)
1559         self.assertEquals(rep.u._pad, '\0' * 3)
1560
1561         # wait for a disconnect
1562         rep = self.recv_pdu()
1563         self.assertIsNone(rep)
1564         self.assertNotConnected()
1565
1566     def _test_auth_none_level_bind(self, auth_level,
1567                                    reason=dcerpc.DCERPC_BIND_NAK_REASON_INVALID_AUTH_TYPE):
1568         return self._test_auth_type_level_bind_nak(auth_type=dcerpc.DCERPC_AUTH_LEVEL_NONE,
1569                                                    auth_level=auth_level, reason=reason)
1570
1571     def test_auth_none_none_bind(self):
1572         return self._test_auth_none_level_bind(dcerpc.DCERPC_AUTH_LEVEL_NONE,
1573                                                reason=dcerpc.DCERPC_BIND_NAK_REASON_NOT_SPECIFIED)
1574
1575     def test_auth_none_connect_bind(self):
1576         return self._test_auth_none_level_bind(dcerpc.DCERPC_AUTH_LEVEL_CONNECT)
1577
1578     def test_auth_none_call_bind(self):
1579         return self._test_auth_none_level_bind(dcerpc.DCERPC_AUTH_LEVEL_CALL)
1580
1581     def test_auth_none_packet_bind(self):
1582         return self._test_auth_none_level_bind(dcerpc.DCERPC_AUTH_LEVEL_PACKET)
1583
1584     def test_auth_none_integrity_bind(self):
1585         return self._test_auth_none_level_bind(dcerpc.DCERPC_AUTH_LEVEL_INTEGRITY)
1586
1587     def test_auth_none_privacy_bind(self):
1588         return self._test_auth_none_level_bind(dcerpc.DCERPC_AUTH_LEVEL_PRIVACY)
1589
1590     def test_auth_none_0_bind(self):
1591         return self._test_auth_none_level_bind(0,
1592                                                reason=dcerpc.DCERPC_BIND_NAK_REASON_NOT_SPECIFIED)
1593
1594     def test_auth_none_7_bind(self):
1595         return self._test_auth_none_level_bind(7,
1596                                                reason=dcerpc.DCERPC_BIND_NAK_REASON_NOT_SPECIFIED)
1597
1598     def test_auth_none_255_bind(self):
1599         return self._test_auth_none_level_bind(255,
1600                                                reason=dcerpc.DCERPC_BIND_NAK_REASON_NOT_SPECIFIED)
1601
1602     def _test_auth_none_level_request(self, auth_level):
1603         ndr32 = base.transfer_syntax_ndr()
1604
1605         tsf1_list = [ndr32]
1606         ctx1 = dcerpc.ctx_list()
1607         ctx1.context_id = 1
1608         ctx1.num_transfer_syntaxes = len(tsf1_list)
1609         ctx1.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
1610         ctx1.transfer_syntaxes = tsf1_list
1611         ctx_list = [ctx1]
1612
1613         auth_type = dcerpc.DCERPC_AUTH_TYPE_NONE
1614         auth_context_id = 0
1615
1616         req = self.generate_bind(call_id=0,
1617                                  ctx_list=ctx_list)
1618
1619         self.send_pdu(req)
1620         rep = self.recv_pdu()
1621         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id)
1622         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
1623         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
1624         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
1625         self.assertEquals(rep.u.secondary_address_size, 4)
1626         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
1627         self.assertEquals(len(rep.u._pad1), 2)
1628         self.assertEquals(rep.u._pad1, '\0' * 2)
1629         self.assertEquals(rep.u.num_results, 1)
1630         self.assertEquals(rep.u.ctx_list[0].result,
1631                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
1632         self.assertEquals(rep.u.ctx_list[0].reason,
1633                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
1634         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
1635         self.assertEquals(len(rep.u.auth_info), 0)
1636
1637         # And now try a request without auth_info
1638         req = self.generate_request(call_id = 2,
1639                                     context_id=ctx1.context_id,
1640                                     opnum=0,
1641                                     stub="")
1642         self.send_pdu(req)
1643         rep = self.recv_pdu()
1644         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1645                         auth_length=0)
1646         self.assertNotEquals(rep.u.alloc_hint, 0)
1647         self.assertEquals(rep.u.context_id, req.u.context_id)
1648         self.assertEquals(rep.u.cancel_count, 0)
1649         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1650
1651         auth_info = self.generate_auth(auth_type=auth_type,
1652                                        auth_level=auth_level,
1653                                        auth_context_id=auth_context_id,
1654                                        auth_blob="none")
1655
1656         req = self.generate_request(call_id = 3,
1657                                     context_id=ctx1.context_id,
1658                                     opnum=0,
1659                                     stub="",
1660                                     auth_info=auth_info)
1661         self.send_pdu(req)
1662         rep = self.recv_pdu()
1663         # We get a fault back
1664         self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
1665                         auth_length=0)
1666         self.assertNotEquals(rep.u.alloc_hint, 0)
1667         self.assertEquals(rep.u.context_id, req.u.context_id)
1668         self.assertEquals(rep.u.cancel_count, 0)
1669         self.assertEquals(rep.u.flags, 0)
1670         self.assertEquals(rep.u.status, dcerpc.DCERPC_FAULT_ACCESS_DENIED)
1671         self.assertEquals(rep.u.reserved, 0)
1672         self.assertEquals(len(rep.u.error_and_verifier), 0)
1673
1674         # wait for a disconnect
1675         rep = self.recv_pdu()
1676         self.assertIsNone(rep)
1677         self.assertNotConnected()
1678
1679     def test_auth_none_none_request(self):
1680         return self._test_auth_none_level_request(dcerpc.DCERPC_AUTH_LEVEL_NONE)
1681
1682     def test_auth_none_connect_request(self):
1683         return self._test_auth_none_level_request(dcerpc.DCERPC_AUTH_LEVEL_CONNECT)
1684
1685     def test_auth_none_call_request(self):
1686         return self._test_auth_none_level_request(dcerpc.DCERPC_AUTH_LEVEL_CALL)
1687
1688     def _test_neg_xmit_check_values(self,
1689                                     req_xmit=None,
1690                                     req_recv=None,
1691                                     rep_both=None,
1692                                     alter_xmit=None,
1693                                     alter_recv=None):
1694         ndr32 = base.transfer_syntax_ndr()
1695
1696         tsf1_list = [ndr32]
1697         ctx1 = dcerpc.ctx_list()
1698         ctx1.context_id = 1
1699         ctx1.num_transfer_syntaxes = len(tsf1_list)
1700         ctx1.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
1701         ctx1.transfer_syntaxes = tsf1_list
1702
1703         req = self.generate_bind(call_id=0,
1704                                  max_xmit_frag=req_xmit,
1705                                  max_recv_frag=req_recv,
1706                                  ctx_list=[ctx1])
1707         self.send_pdu(req)
1708         rep = self.recv_pdu()
1709         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
1710                         auth_length=0)
1711         self.assertEquals(rep.u.max_xmit_frag, rep_both)
1712         self.assertEquals(rep.u.max_recv_frag, rep_both)
1713         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
1714         self.assertEquals(rep.u.secondary_address_size, 4)
1715         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
1716         self.assertEquals(len(rep.u._pad1), 2)
1717         self.assertEquals(rep.u._pad1, '\0' * 2)
1718         self.assertEquals(rep.u.num_results, 1)
1719         self.assertEquals(rep.u.ctx_list[0].result,
1720                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
1721         self.assertEquals(rep.u.ctx_list[0].reason,
1722                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
1723         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
1724         self.assertEquals(rep.u.auth_info, '\0' * 0)
1725
1726         assoc_group_id = rep.u.assoc_group_id
1727         if alter_xmit is None:
1728             alter_xmit = rep_both - 8
1729         if alter_recv is None:
1730             alter_recv = rep_both - 8
1731
1732         # max_{xmit,recv}_frag and assoc_group_id are completely
1733         # ignored in alter_context requests
1734         req = self.generate_alter(call_id=1,
1735                                   max_xmit_frag=alter_xmit,
1736                                   max_recv_frag=alter_recv,
1737                                   assoc_group_id=0xffffffff - rep.u.assoc_group_id,
1738                                   ctx_list=[ctx1])
1739         self.send_pdu(req)
1740         rep = self.recv_pdu()
1741         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id,
1742                         auth_length=0)
1743         self.assertEquals(rep.u.max_xmit_frag, rep_both)
1744         self.assertEquals(rep.u.max_recv_frag, rep_both)
1745         self.assertEquals(rep.u.assoc_group_id, rep.u.assoc_group_id)
1746         self.assertEquals(rep.u.secondary_address_size, 0)
1747         self.assertEquals(len(rep.u._pad1), 2)
1748         #self.assertEquals(rep.u._pad1, '\0' * 2)
1749         self.assertEquals(rep.u.num_results, 1)
1750         self.assertEquals(rep.u.ctx_list[0].result,
1751                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
1752         self.assertEquals(rep.u.ctx_list[0].reason,
1753                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
1754         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
1755         self.assertEquals(rep.u.auth_info, '\0' * 0)
1756
1757         chunk_size = rep_both - dcerpc.DCERPC_REQUEST_LENGTH
1758         req = self.generate_request(call_id = 2,
1759                                     context_id=ctx1.context_id,
1760                                     opnum=0,
1761                                     alloc_hint=0xffffffff,
1762                                     stub="\00" * chunk_size)
1763         self.send_pdu(req,ndr_print=True,hexdump=True)
1764         rep = self.recv_pdu(ndr_print=True,hexdump=True)
1765         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1766                         auth_length=0)
1767         self.assertNotEquals(rep.u.alloc_hint, 0)
1768         self.assertEquals(rep.u.context_id, req.u.context_id)
1769         self.assertEquals(rep.u.cancel_count, 0)
1770         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1771
1772         chunk_size = 5840 - dcerpc.DCERPC_REQUEST_LENGTH
1773         req = self.generate_request(call_id = 2,
1774                                     context_id=ctx1.context_id,
1775                                     opnum=0,
1776                                     alloc_hint=0xffffffff,
1777                                     stub="\00" * chunk_size)
1778         self.send_pdu(req)
1779         rep = self.recv_pdu()
1780         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1781                         auth_length=0)
1782         self.assertNotEquals(rep.u.alloc_hint, 0)
1783         self.assertEquals(rep.u.context_id, req.u.context_id)
1784         self.assertEquals(rep.u.cancel_count, 0)
1785         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1786
1787         chunk_size += 1
1788         req = self.generate_request(call_id = 3,
1789                                     context_id=ctx1.context_id,
1790                                     opnum=0,
1791                                     alloc_hint=0xffffffff,
1792                                     stub="\00" * chunk_size)
1793         self.send_pdu(req)
1794         rep = self.recv_pdu()
1795         # We get a fault
1796         self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
1797                         auth_length=0)
1798         self.assertNotEquals(rep.u.alloc_hint, 0)
1799         self.assertEquals(rep.u.context_id, 0)
1800         self.assertEquals(rep.u.cancel_count, 0)
1801         self.assertEquals(rep.u.flags, 0)
1802         self.assertEquals(rep.u.status, dcerpc.DCERPC_NCA_S_PROTO_ERROR)
1803         self.assertEquals(rep.u.reserved, 0)
1804         self.assertEquals(len(rep.u.error_and_verifier), 0)
1805
1806         # wait for a disconnect
1807         rep = self.recv_pdu()
1808         self.assertIsNone(rep)
1809         self.assertNotConnected()
1810
1811     def test_neg_xmit_ffff_ffff(self):
1812         return self._test_neg_xmit_check_values(req_xmit=0xffff,
1813                                                 req_recv=0xffff,
1814                                                 rep_both=5840)
1815
1816     def test_neg_xmit_0_ffff(self):
1817         return self._test_neg_xmit_check_values(req_xmit=0,
1818                                                 req_recv=0xffff,
1819                                                 rep_both=2048,
1820                                                 alter_xmit=0xffff,
1821                                                 alter_recv=0xffff)
1822
1823     def test_neg_xmit_ffff_0(self):
1824         return self._test_neg_xmit_check_values(req_xmit=0xffff,
1825                                                 req_recv=0,
1826                                                 rep_both=2048)
1827
1828     def test_neg_xmit_0_0(self):
1829         return self._test_neg_xmit_check_values(req_xmit=0,
1830                                                 req_recv=0,
1831                                                 rep_both=2048,
1832                                                 alter_xmit=0xffff,
1833                                                 alter_recv=0xffff)
1834
1835     def test_neg_xmit_3199_0(self):
1836         return self._test_neg_xmit_check_values(req_xmit=3199,
1837                                                 req_recv=0,
1838                                                 rep_both=2048)
1839     def test_neg_xmit_0_3199(self):
1840         return self._test_neg_xmit_check_values(req_xmit=0,
1841                                                 req_recv=3199,
1842                                                 rep_both=2048)
1843
1844     def test_neg_xmit_3199_ffff(self):
1845         return self._test_neg_xmit_check_values(req_xmit=3199,
1846                                                 req_recv=0xffff,
1847                                                 rep_both=3192)
1848     def test_neg_xmit_ffff_3199(self):
1849         return self._test_neg_xmit_check_values(req_xmit=0xffff,
1850                                                 req_recv=3199,
1851                                                 rep_both=3192)
1852
1853     def test_alloc_hint(self):
1854         ndr32 = base.transfer_syntax_ndr()
1855
1856         tsf1_list = [ndr32]
1857         ctx = dcerpc.ctx_list()
1858         ctx.context_id = 0
1859         ctx.num_transfer_syntaxes = len(tsf1_list)
1860         ctx.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
1861         ctx.transfer_syntaxes = tsf1_list
1862
1863         req = self.generate_bind(call_id=0,
1864                                  ctx_list=[ctx])
1865         self.send_pdu(req)
1866         rep = self.recv_pdu()
1867         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id,
1868                         auth_length=0)
1869         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
1870         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
1871         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
1872         self.assertEquals(rep.u.secondary_address_size, 4)
1873         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
1874         self.assertEquals(len(rep.u._pad1), 2)
1875         self.assertEquals(rep.u._pad1, '\0' * 2)
1876         self.assertEquals(rep.u.num_results, 1)
1877         self.assertEquals(rep.u.ctx_list[0].result,
1878                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
1879         self.assertEquals(rep.u.ctx_list[0].reason,
1880                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
1881         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
1882         self.assertEquals(rep.u.auth_info, '\0' * 0)
1883
1884         # And now try a request without auth_info
1885         req = self.generate_request(call_id = 2,
1886                                     context_id=ctx.context_id,
1887                                     opnum=0,
1888                                     alloc_hint=0xffffffff,
1889                                     stub="")
1890         self.send_pdu(req)
1891         rep = self.recv_pdu()
1892         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1893                         auth_length=0)
1894         self.assertNotEquals(rep.u.alloc_hint, 0)
1895         self.assertEquals(rep.u.context_id, req.u.context_id)
1896         self.assertEquals(rep.u.cancel_count, 0)
1897         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1898
1899         req = self.generate_request(call_id = 3,
1900                                     context_id=ctx.context_id,
1901                                     opnum=1,
1902                                     alloc_hint=0xffffffff,
1903                                     stub="\04\00\00\00\00\00\00\00")
1904         self.send_pdu(req)
1905         rep = self.recv_pdu()
1906         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1907                         auth_length=0)
1908         self.assertNotEquals(rep.u.alloc_hint, 0)
1909         self.assertEquals(rep.u.context_id, req.u.context_id)
1910         self.assertEquals(rep.u.cancel_count, 0)
1911         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1912
1913         req = self.generate_request(call_id = 4,
1914                                     context_id=ctx.context_id,
1915                                     opnum=1,
1916                                     alloc_hint=1,
1917                                     stub="\04\00\00\00\00\00\00\00")
1918         self.send_pdu(req)
1919         rep = self.recv_pdu()
1920         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
1921                         auth_length=0)
1922         self.assertNotEquals(rep.u.alloc_hint, 0)
1923         self.assertEquals(rep.u.context_id, req.u.context_id)
1924         self.assertEquals(rep.u.cancel_count, 0)
1925         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
1926
1927     def _get_netlogon_ctx(self):
1928         abstract = samba.dcerpc.netlogon.abstract_syntax()
1929         ndr32 = base.transfer_syntax_ndr()
1930
1931         (ctx, ack) = self.prepare_presentation(abstract, ndr32, context_id=0,
1932                                                epmap=True, return_ack=True)
1933
1934         server = '\\\\' + self.target_hostname
1935         server_utf16 = unicode(server, 'utf-8').encode('utf-16-le')
1936         computer = 'UNKNOWNCOMPUTER'
1937         computer_utf16 = unicode(computer, 'utf-8').encode('utf-16-le')
1938
1939         real_stub = struct.pack('<IIII', 0x00200000,
1940                                  len(server) + 1, 0, len(server) + 1)
1941         real_stub += server_utf16 + '\x00\x00'
1942         mod_len = len(real_stub) % 4
1943         if mod_len != 0:
1944             real_stub += '\x00' * (4 - mod_len)
1945         real_stub += struct.pack('<III',
1946                                  len(computer) + 1, 0, len(computer) + 1)
1947         real_stub += computer_utf16 + '\x00\x00'
1948         real_stub += '\x11\x22\x33\x44\x55\x66\x77\x88'
1949
1950         return (ctx, ack, real_stub)
1951
1952     def _test_fragmented_requests(self, remaining=None, alloc_hint=None,
1953                                   fault_first=None, fault_last=None):
1954         (ctx, rep, real_stub) = self._get_netlogon_ctx()
1955
1956         chunk = rep.u.max_recv_frag - dcerpc.DCERPC_REQUEST_LENGTH
1957
1958         total = 0
1959         first = True
1960         while remaining > 0:
1961             thistime = min(remaining, chunk)
1962             remaining -= thistime
1963             total += thistime
1964
1965             pfc_flags = 0
1966             if first:
1967                 pfc_flags |= dcerpc.DCERPC_PFC_FLAG_FIRST
1968                 first = False
1969                 stub = real_stub + '\x00' * (thistime - len(real_stub))
1970             else:
1971                 stub = "\x00" * thistime
1972
1973             if remaining == 0:
1974                 pfc_flags |= dcerpc.DCERPC_PFC_FLAG_LAST
1975
1976             # And now try a request without auth_info
1977             # netr_ServerReqChallenge()
1978             req = self.generate_request(call_id = 0x21234,
1979                                         pfc_flags=pfc_flags,
1980                                         context_id=ctx.context_id,
1981                                         opnum=4,
1982                                         alloc_hint=alloc_hint,
1983                                         stub=stub)
1984             if alloc_hint >= thistime:
1985                 alloc_hint -= thistime
1986             else:
1987                 alloc_hint = 0
1988             self.send_pdu(req,hexdump=False)
1989             if fault_first is not None:
1990                 rep = self.recv_pdu()
1991                 # We get a fault back
1992                 self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
1993                                 auth_length=0)
1994                 self.assertNotEquals(rep.u.alloc_hint, 0)
1995                 self.assertEquals(rep.u.context_id, req.u.context_id)
1996                 self.assertEquals(rep.u.cancel_count, 0)
1997                 self.assertEquals(rep.u.flags, 0)
1998                 self.assertEquals(rep.u.status, fault_first)
1999                 self.assertEquals(rep.u.reserved, 0)
2000                 self.assertEquals(len(rep.u.error_and_verifier), 0)
2001
2002                 # wait for a disconnect
2003                 rep = self.recv_pdu()
2004                 self.assertIsNone(rep)
2005                 self.assertNotConnected()
2006                 return
2007             if remaining == 0:
2008                 break
2009             if total >= 0x400000 and fault_last is not None:
2010                 rep = self.recv_pdu()
2011                 # We get a fault back
2012                 self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
2013                                 auth_length=0)
2014                 self.assertNotEquals(rep.u.alloc_hint, 0)
2015                 self.assertEquals(rep.u.context_id, req.u.context_id)
2016                 self.assertEquals(rep.u.cancel_count, 0)
2017                 self.assertEquals(rep.u.flags, 0)
2018                 self.assertEquals(rep.u.status, fault_last)
2019                 self.assertEquals(rep.u.reserved, 0)
2020                 self.assertEquals(len(rep.u.error_and_verifier), 0)
2021
2022                 # wait for a disconnect
2023                 rep = self.recv_pdu()
2024                 self.assertIsNone(rep)
2025                 self.assertNotConnected()
2026                 return
2027             rep = self.recv_pdu(timeout=0.01)
2028             self.assertIsNone(rep)
2029             self.assertIsConnected()
2030
2031         if total >= 0x400000 and fault_last is not None:
2032             rep = self.recv_pdu()
2033             # We get a fault back
2034             self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
2035                             auth_length=0)
2036             self.assertNotEquals(rep.u.alloc_hint, 0)
2037             self.assertEquals(rep.u.context_id, req.u.context_id)
2038             self.assertEquals(rep.u.cancel_count, 0)
2039             self.assertEquals(rep.u.flags, 0)
2040             self.assertEquals(rep.u.status, fault_last)
2041             self.assertEquals(rep.u.reserved, 0)
2042             self.assertEquals(len(rep.u.error_and_verifier), 0)
2043
2044             # wait for a disconnect
2045             rep = self.recv_pdu()
2046             self.assertIsNone(rep)
2047             self.assertNotConnected()
2048             return
2049         rep = self.recv_pdu()
2050         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
2051                         auth_length=0)
2052         self.assertNotEquals(rep.u.alloc_hint, 0)
2053         self.assertEquals(rep.u.context_id, req.u.context_id)
2054         self.assertEquals(rep.u.cancel_count, 0)
2055         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
2056
2057         self.assertEquals(len(rep.u.stub_and_verifier), 12)
2058         status = struct.unpack_from("<I", rep.u.stub_and_verifier, len(rep.u.stub_and_verifier) - 4)
2059         self.assertEquals(status[0], 0)
2060
2061     def test_fragmented_requests01(self):
2062         return self._test_fragmented_requests(remaining=0x400000,
2063                                               alloc_hint=0x400000)
2064
2065     def test_fragmented_requests02(self):
2066         return self._test_fragmented_requests(remaining=0x400000,
2067                                               alloc_hint=0x100000)
2068
2069     def test_fragmented_requests03(self):
2070         return self._test_fragmented_requests(remaining=0x400000,
2071                                               alloc_hint=0)
2072
2073     def test_fragmented_requests04(self):
2074         return self._test_fragmented_requests(remaining=0x400000,
2075                                               alloc_hint=0x400001,
2076                                               fault_first=dcerpc.DCERPC_FAULT_ACCESS_DENIED)
2077
2078     def test_fragmented_requests05(self):
2079         return self._test_fragmented_requests(remaining=0x500001,
2080                                               alloc_hint=0,
2081                                               fault_last=dcerpc.DCERPC_FAULT_ACCESS_DENIED)
2082
2083     def _test_same_requests(self, pfc_flags, fault_1st=False, fault_2nd=False):
2084         (ctx, rep, real_stub) = self._get_netlogon_ctx()
2085
2086         # netr_ServerReqChallenge with given flags
2087         req = self.generate_request(call_id = 2,
2088                                     pfc_flags=pfc_flags,
2089                                     context_id=ctx.context_id,
2090                                     opnum=4,
2091                                     stub=real_stub)
2092         self.send_pdu(req)
2093         if fault_1st:
2094             rep = self.recv_pdu()
2095             # We get a fault back
2096             self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
2097                             auth_length=0)
2098             self.assertNotEquals(rep.u.alloc_hint, 0)
2099             self.assertEquals(rep.u.context_id, 0)
2100             self.assertEquals(rep.u.cancel_count, 0)
2101             self.assertEquals(rep.u.flags, 0)
2102             self.assertEquals(rep.u.status, dcerpc.DCERPC_NCA_S_PROTO_ERROR)
2103             self.assertEquals(rep.u.reserved, 0)
2104             self.assertEquals(len(rep.u.error_and_verifier), 0)
2105
2106             # wait for a disconnect
2107             rep = self.recv_pdu()
2108             self.assertIsNone(rep)
2109             self.assertNotConnected()
2110             return
2111         rep = self.recv_pdu(timeout=0.1)
2112         self.assertIsNone(rep)
2113         self.assertIsConnected()
2114
2115         # netr_ServerReqChallenge without DCERPC_PFC_FLAG_LAST
2116         # with the same call_id
2117         req = self.generate_request(call_id = 2,
2118                                     pfc_flags=pfc_flags,
2119                                     context_id=ctx.context_id,
2120                                     opnum=4,
2121                                     stub=real_stub)
2122         self.send_pdu(req)
2123         if fault_2nd:
2124             rep = self.recv_pdu()
2125             # We get a fault back
2126             self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
2127                             auth_length=0)
2128             self.assertNotEquals(rep.u.alloc_hint, 0)
2129             self.assertEquals(rep.u.context_id, req.u.context_id)
2130             self.assertEquals(rep.u.cancel_count, 0)
2131             self.assertEquals(rep.u.flags, 0)
2132             self.assertEquals(rep.u.status, dcerpc.DCERPC_NCA_S_PROTO_ERROR)
2133             self.assertEquals(rep.u.reserved, 0)
2134             self.assertEquals(len(rep.u.error_and_verifier), 0)
2135
2136             # wait for a disconnect
2137             rep = self.recv_pdu()
2138             self.assertIsNone(rep)
2139             self.assertNotConnected()
2140             return
2141
2142         rep = self.recv_pdu(timeout=0.1)
2143         self.assertIsNone(rep)
2144         self.assertIsConnected()
2145
2146     def test_first_only_requests(self):
2147         return self._test_same_requests(pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST,
2148                                         fault_2nd=True)
2149
2150     def test_none_only_requests(self):
2151         return self._test_same_requests(pfc_flags=0, fault_1st=True)
2152
2153     def test_last_only_requests(self):
2154         return self._test_same_requests(pfc_flags=dcerpc.DCERPC_PFC_FLAG_LAST,
2155                                         fault_1st=True)
2156
2157     def test_first_maybe_requests(self):
2158         return self._test_same_requests(pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST |
2159                                         dcerpc.DCERPC_PFC_FLAG_MAYBE,
2160                                         fault_2nd=True)
2161
2162     def test_first_didnot_requests(self):
2163         return self._test_same_requests(pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST |
2164                                         dcerpc.DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
2165                                         fault_2nd=True)
2166
2167     def test_first_cmpx_requests(self):
2168         return self._test_same_requests(pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST |
2169                                         dcerpc.DCERPC_PFC_FLAG_CONC_MPX,
2170                                         fault_2nd=True)
2171
2172     def test_first_08_requests(self):
2173         return self._test_same_requests(pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST |
2174                                         0x08,
2175                                         fault_2nd=True)
2176
2177     def test_first_cancel_requests(self):
2178         (ctx, rep, real_stub) = self._get_netlogon_ctx()
2179
2180         # netr_ServerReqChallenge with given flags
2181         req = self.generate_request(call_id = 2,
2182                                     pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST |
2183                                     dcerpc.DCERPC_PFC_FLAG_PENDING_CANCEL,
2184                                     context_id=ctx.context_id,
2185                                     opnum=4,
2186                                     stub=real_stub)
2187         self.send_pdu(req)
2188         rep = self.recv_pdu()
2189         # We get a fault back
2190         self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
2191                         pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST |
2192                         dcerpc.DCERPC_PFC_FLAG_LAST |
2193                         dcerpc.DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
2194                         auth_length=0)
2195         self.assertNotEquals(rep.u.alloc_hint, 0)
2196         self.assertEquals(rep.u.context_id, 0)
2197         self.assertEquals(rep.u.cancel_count, 0)
2198         self.assertEquals(rep.u.flags, 0)
2199         self.assertEquals(rep.u.status, dcerpc.DCERPC_FAULT_NO_CALL_ACTIVE)
2200         self.assertEquals(rep.u.reserved, 0)
2201         self.assertEquals(len(rep.u.error_and_verifier), 0)
2202
2203         # wait for a disconnect
2204         rep = self.recv_pdu()
2205         self.assertIsNone(rep)
2206         self.assertNotConnected()
2207
2208     def test_2nd_cancel_requests(self):
2209         (ctx, rep, real_stub) = self._get_netlogon_ctx()
2210
2211         # netr_ServerReqChallenge with given flags
2212         req = self.generate_request(call_id = 2,
2213                                     pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST,
2214                                     context_id=ctx.context_id,
2215                                     opnum=4,
2216                                     stub=real_stub)
2217         self.send_pdu(req)
2218         rep = self.recv_pdu(timeout=0.1)
2219         self.assertIsNone(rep)
2220         self.assertIsConnected()
2221
2222         # netr_ServerReqChallenge with given flags
2223         req = self.generate_request(call_id = 2,
2224                                     pfc_flags=dcerpc.DCERPC_PFC_FLAG_PENDING_CANCEL,
2225                                     context_id=ctx.context_id,
2226                                     opnum=4,
2227                                     stub=real_stub)
2228         self.send_pdu(req)
2229         rep = self.recv_pdu(timeout=0.1)
2230         self.assertIsNone(rep)
2231         self.assertIsConnected()
2232
2233         # netr_ServerReqChallenge with given flags
2234         req = self.generate_request(call_id = 2,
2235                                     pfc_flags=dcerpc.DCERPC_PFC_FLAG_LAST,
2236                                     context_id=ctx.context_id,
2237                                     opnum=4,
2238                                     stub=real_stub)
2239         self.send_pdu(req)
2240         rep = self.recv_pdu()
2241         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
2242                         auth_length=0)
2243         self.assertNotEquals(rep.u.alloc_hint, 0)
2244         self.assertEquals(rep.u.context_id, req.u.context_id)
2245         self.assertEquals(rep.u.cancel_count, 0)
2246         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
2247
2248         self.assertEquals(len(rep.u.stub_and_verifier), 12)
2249         status = struct.unpack_from("<I", rep.u.stub_and_verifier, len(rep.u.stub_and_verifier) - 4)
2250         self.assertEquals(status[0], 0)
2251
2252     def test_last_cancel_requests(self):
2253         (ctx, rep, real_stub) = self._get_netlogon_ctx()
2254
2255         # netr_ServerReqChallenge with given flags
2256         req = self.generate_request(call_id = 2,
2257                                     pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST,
2258                                     context_id=ctx.context_id,
2259                                     opnum=4,
2260                                     stub=real_stub[:4])
2261         self.send_pdu(req)
2262         rep = self.recv_pdu(timeout=0.1)
2263         self.assertIsNone(rep)
2264         self.assertIsConnected()
2265
2266         # netr_ServerReqChallenge with given flags
2267         req = self.generate_request(call_id = 2,
2268                                     pfc_flags=dcerpc.DCERPC_PFC_FLAG_LAST |
2269                                     dcerpc.DCERPC_PFC_FLAG_PENDING_CANCEL,
2270                                     context_id=ctx.context_id,
2271                                     opnum=4,
2272                                     stub=real_stub[4:])
2273         self.send_pdu(req)
2274         rep = self.recv_pdu()
2275         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
2276                         auth_length=0)
2277         self.assertNotEquals(rep.u.alloc_hint, 0)
2278         self.assertEquals(rep.u.context_id, req.u.context_id)
2279         self.assertEquals(rep.u.cancel_count, 0)
2280         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
2281
2282         self.assertEquals(len(rep.u.stub_and_verifier), 12)
2283         status = struct.unpack_from("<I", rep.u.stub_and_verifier, len(rep.u.stub_and_verifier) - 4)
2284         self.assertEquals(status[0], 0)
2285
2286     def test_mix_requests(self):
2287         (ctx, rep, real_stub) = self._get_netlogon_ctx()
2288
2289         # netr_ServerReqChallenge with given flags
2290         req = self.generate_request(call_id = 50,
2291                                     pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST,
2292                                     context_id=ctx.context_id,
2293                                     opnum=4,
2294                                     stub=real_stub)
2295         self.send_pdu(req)
2296         rep = self.recv_pdu(timeout=0.1)
2297         self.assertIsNone(rep)
2298         self.assertIsConnected()
2299
2300         # netr_ServerReqChallenge with given flags
2301         req = self.generate_request(call_id = 51,
2302                                     pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST,
2303                                     context_id=ctx.context_id,
2304                                     opnum=4,
2305                                     stub=real_stub)
2306         self.send_pdu(req)
2307         rep = self.recv_pdu()
2308         # We get a fault back
2309         self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, 50,
2310                         pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST |
2311                         dcerpc.DCERPC_PFC_FLAG_LAST,
2312                         auth_length=0)
2313         self.assertNotEquals(rep.u.alloc_hint, 0)
2314         self.assertEquals(rep.u.context_id, req.u.context_id)
2315         self.assertEquals(rep.u.cancel_count, 0)
2316         self.assertEquals(rep.u.flags, 0)
2317         self.assertEquals(rep.u.status, dcerpc.DCERPC_NCA_S_PROTO_ERROR)
2318         self.assertEquals(rep.u.reserved, 0)
2319         self.assertEquals(len(rep.u.error_and_verifier), 0)
2320
2321     def test_co_cancel_no_request(self):
2322         ndr32 = base.transfer_syntax_ndr()
2323         abstract = samba.dcerpc.mgmt.abstract_syntax()
2324         ctx = self.prepare_presentation(abstract, ndr32, context_id=0xff)
2325
2326         req = self.generate_co_cancel(call_id = 3)
2327         self.send_pdu(req)
2328         rep = self.recv_pdu(timeout=0.01)
2329         self.assertIsNone(rep)
2330         self.assertIsConnected()
2331
2332         # And now try a request
2333         req = self.generate_request(call_id = 1,
2334                                     context_id=ctx.context_id,
2335                                     opnum=0,
2336                                     stub="")
2337         self.send_pdu(req)
2338         rep = self.recv_pdu()
2339         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
2340                         auth_length=0)
2341         self.assertNotEquals(rep.u.alloc_hint, 0)
2342         self.assertEquals(rep.u.context_id, req.u.context_id)
2343         self.assertEquals(rep.u.cancel_count, 0)
2344         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
2345
2346     def test_co_cancel_request_after_first(self):
2347         ndr32 = base.transfer_syntax_ndr()
2348         abstract = samba.dcerpc.mgmt.abstract_syntax()
2349         ctx = self.prepare_presentation(abstract, ndr32, context_id=0xff)
2350
2351         req = self.generate_request(call_id = 1,
2352                                     pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST,
2353                                     context_id=ctx.context_id,
2354                                     opnum=0,
2355                                     stub="")
2356         self.send_pdu(req)
2357         rep = self.recv_pdu(timeout=0.01)
2358         self.assertIsNone(rep)
2359         self.assertIsConnected()
2360
2361         req = self.generate_co_cancel(call_id = 1)
2362         self.send_pdu(req)
2363         rep = self.recv_pdu(timeout=0.01)
2364         self.assertIsNone(rep)
2365         self.assertIsConnected()
2366
2367         req = self.generate_request(call_id = 1,
2368                                     pfc_flags=dcerpc.DCERPC_PFC_FLAG_LAST,
2369                                     context_id=ctx.context_id,
2370                                     opnum=0,
2371                                     stub="")
2372         self.send_pdu(req)
2373         rep = self.recv_pdu()
2374         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
2375                         auth_length=0)
2376         self.assertNotEquals(rep.u.alloc_hint, 0)
2377         self.assertEquals(rep.u.context_id, req.u.context_id)
2378         self.assertEquals(rep.u.cancel_count, 0)
2379         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
2380
2381         # And now try a request
2382         req = self.generate_request(call_id = 2,
2383                                     context_id=ctx.context_id,
2384                                     opnum=0,
2385                                     stub="")
2386         self.send_pdu(req)
2387         rep = self.recv_pdu()
2388         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
2389                         auth_length=0)
2390         self.assertNotEquals(rep.u.alloc_hint, 0)
2391         self.assertEquals(rep.u.context_id, req.u.context_id)
2392         self.assertEquals(rep.u.cancel_count, 0)
2393         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
2394
2395     def test_orphaned_no_request(self):
2396         ndr32 = base.transfer_syntax_ndr()
2397         abstract = samba.dcerpc.mgmt.abstract_syntax()
2398         ctx = self.prepare_presentation(abstract, ndr32)
2399
2400         req = self.generate_orphaned(call_id = 3)
2401         self.send_pdu(req)
2402         rep = self.recv_pdu(timeout=0.01)
2403         self.assertIsNone(rep)
2404         self.assertIsConnected()
2405
2406         # And now try a request
2407         req = self.generate_request(call_id = 1,
2408                                     context_id=ctx.context_id,
2409                                     opnum=0,
2410                                     stub="")
2411         self.send_pdu(req)
2412         rep = self.recv_pdu()
2413         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
2414                         auth_length=0)
2415         self.assertNotEquals(rep.u.alloc_hint, 0)
2416         self.assertEquals(rep.u.context_id, req.u.context_id & 0xff)
2417         self.assertEquals(rep.u.cancel_count, 0)
2418         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
2419
2420     def test_orphaned_request_after_first_last(self):
2421         ndr32 = base.transfer_syntax_ndr()
2422         abstract = samba.dcerpc.mgmt.abstract_syntax()
2423         ctx = self.prepare_presentation(abstract, ndr32)
2424
2425         req = self.generate_request(call_id = 1,
2426                                     pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST,
2427                                     context_id=ctx.context_id,
2428                                     opnum=0,
2429                                     stub="")
2430         self.send_pdu(req)
2431         rep = self.recv_pdu(timeout=0.1)
2432         self.assertIsNone(rep)
2433         self.assertIsConnected()
2434
2435         req = self.generate_orphaned(call_id = 1)
2436         self.send_pdu(req)
2437         rep = self.recv_pdu(timeout=0.1)
2438         self.assertIsNone(rep)
2439         self.assertIsConnected()
2440
2441         req = self.generate_request(call_id = 1,
2442                                     pfc_flags=dcerpc.DCERPC_PFC_FLAG_LAST,
2443                                     context_id=ctx.context_id,
2444                                     opnum=0,
2445                                     stub="")
2446         self.send_pdu(req)
2447         rep = self.recv_pdu()
2448         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
2449                         auth_length=0)
2450         self.assertNotEquals(rep.u.alloc_hint, 0)
2451         self.assertEquals(rep.u.context_id, req.u.context_id & 0xff)
2452         self.assertEquals(rep.u.cancel_count, 0)
2453         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
2454
2455         # And now try a request
2456         req = self.generate_request(call_id = 2,
2457                                     context_id=ctx.context_id,
2458                                     opnum=0,
2459                                     stub="")
2460         self.send_pdu(req)
2461         rep = self.recv_pdu()
2462         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
2463                         auth_length=0)
2464         self.assertNotEquals(rep.u.alloc_hint, 0)
2465         self.assertEquals(rep.u.context_id, req.u.context_id & 0xff)
2466         self.assertEquals(rep.u.cancel_count, 0)
2467         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
2468
2469     def test_orphaned_request_after_first_mpx_last(self):
2470         ndr32 = base.transfer_syntax_ndr()
2471         abstract = samba.dcerpc.mgmt.abstract_syntax()
2472
2473         pfc_flags = samba.dcerpc.dcerpc.DCERPC_PFC_FLAG_FIRST
2474         pfc_flags |= samba.dcerpc.dcerpc.DCERPC_PFC_FLAG_LAST
2475         pfc_flags |= samba.dcerpc.dcerpc.DCERPC_PFC_FLAG_CONC_MPX
2476         ctx = self.prepare_presentation(abstract, ndr32, pfc_flags=pfc_flags)
2477
2478         req = self.generate_request(call_id = 1,
2479                                     pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST,
2480                                     context_id=ctx.context_id,
2481                                     opnum=0,
2482                                     stub="")
2483         self.send_pdu(req)
2484         rep = self.recv_pdu(timeout=0.1)
2485         self.assertIsNone(rep)
2486         self.assertIsConnected()
2487
2488         req = self.generate_orphaned(call_id = 1)
2489         self.send_pdu(req)
2490         rep = self.recv_pdu(timeout=0.1)
2491         self.assertIsNone(rep)
2492         self.assertIsConnected()
2493
2494         req = self.generate_request(call_id = 1,
2495                                     pfc_flags=dcerpc.DCERPC_PFC_FLAG_LAST,
2496                                     context_id=ctx.context_id,
2497                                     opnum=0,
2498                                     stub="")
2499         self.send_pdu(req)
2500         rep = self.recv_pdu()
2501         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
2502                         auth_length=0)
2503         self.assertNotEquals(rep.u.alloc_hint, 0)
2504         self.assertEquals(rep.u.context_id, req.u.context_id & 0xff)
2505         self.assertEquals(rep.u.cancel_count, 0)
2506         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
2507
2508         # And now try a request
2509         req = self.generate_request(call_id = 2,
2510                                     context_id=ctx.context_id,
2511                                     opnum=0,
2512                                     stub="")
2513         self.send_pdu(req)
2514         rep = self.recv_pdu()
2515         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
2516                         auth_length=0)
2517         self.assertNotEquals(rep.u.alloc_hint, 0)
2518         self.assertEquals(rep.u.context_id, req.u.context_id & 0xff)
2519         self.assertEquals(rep.u.cancel_count, 0)
2520         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
2521
2522     def test_orphaned_request_after_first_no_last(self):
2523         ndr32 = base.transfer_syntax_ndr()
2524         abstract = samba.dcerpc.mgmt.abstract_syntax()
2525         ctx = self.prepare_presentation(abstract, ndr32)
2526
2527         req1 = self.generate_request(call_id = 1,
2528                                      pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST,
2529                                      context_id=ctx.context_id,
2530                                      opnum=0,
2531                                      stub="")
2532         self.send_pdu(req1)
2533         rep = self.recv_pdu(timeout=0.1)
2534         self.assertIsNone(rep)
2535         self.assertIsConnected()
2536
2537         req = self.generate_orphaned(call_id = 1)
2538         self.send_pdu(req)
2539         rep = self.recv_pdu(timeout=0.1)
2540         self.assertIsNone(rep)
2541         self.assertIsConnected()
2542
2543         # And now try a new request
2544         req2 = self.generate_request(call_id = 2,
2545                                      context_id=ctx.context_id,
2546                                      opnum=0,
2547                                      stub="")
2548         self.send_pdu(req2)
2549         rep = self.recv_pdu()
2550         self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req1.call_id,
2551                         auth_length=0)
2552         self.assertNotEquals(rep.u.alloc_hint, 0)
2553         self.assertEquals(rep.u.context_id, req1.u.context_id)
2554         self.assertEquals(rep.u.cancel_count, 0)
2555         self.assertEquals(rep.u.flags, 0)
2556         self.assertEquals(rep.u.status, dcerpc.DCERPC_NCA_S_PROTO_ERROR)
2557         self.assertEquals(rep.u.reserved, 0)
2558         self.assertEquals(len(rep.u.error_and_verifier), 0)
2559
2560         # wait for a disconnect
2561         rep = self.recv_pdu()
2562         self.assertIsNone(rep)
2563         self.assertNotConnected()
2564
2565     def test_orphaned_request_after_first_mpx_no_last(self):
2566         ndr32 = base.transfer_syntax_ndr()
2567         abstract = samba.dcerpc.mgmt.abstract_syntax()
2568
2569         pfc_flags = samba.dcerpc.dcerpc.DCERPC_PFC_FLAG_FIRST
2570         pfc_flags |= samba.dcerpc.dcerpc.DCERPC_PFC_FLAG_LAST
2571         pfc_flags |= samba.dcerpc.dcerpc.DCERPC_PFC_FLAG_CONC_MPX
2572         ctx = self.prepare_presentation(abstract, ndr32,
2573                                         pfc_flags=pfc_flags)
2574
2575         req1 = self.generate_request(call_id = 1,
2576                                      pfc_flags=dcerpc.DCERPC_PFC_FLAG_FIRST,
2577                                      context_id=ctx.context_id,
2578                                      opnum=0,
2579                                      stub="")
2580         self.send_pdu(req1)
2581         rep = self.recv_pdu(timeout=0.1)
2582         self.assertIsNone(rep)
2583         self.assertIsConnected()
2584
2585         req = self.generate_orphaned(call_id = 1)
2586         self.send_pdu(req)
2587         rep = self.recv_pdu(timeout=0.1)
2588         self.assertIsNone(rep)
2589         self.assertIsConnected()
2590
2591         # And now try a new request
2592         req2 = self.generate_request(call_id = 2,
2593                                      context_id=ctx.context_id - 1,
2594                                      opnum=0,
2595                                      stub="")
2596         self.send_pdu(req2)
2597         rep = self.recv_pdu()
2598         self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req2.call_id,
2599                         auth_length=0)
2600         self.assertNotEquals(rep.u.alloc_hint, 0)
2601         self.assertEquals(rep.u.context_id, 0)
2602         self.assertEquals(rep.u.cancel_count, 0)
2603         self.assertEquals(rep.u.flags, 0)
2604         self.assertEquals(rep.u.status, dcerpc.DCERPC_NCA_S_PROTO_ERROR)
2605         self.assertEquals(rep.u.reserved, 0)
2606         self.assertEquals(len(rep.u.error_and_verifier), 0)
2607
2608         # wait for a disconnect
2609         rep = self.recv_pdu()
2610         self.assertIsNone(rep)
2611         self.assertNotConnected()
2612
2613     def test_spnego_connect_request(self):
2614         ndr32 = base.transfer_syntax_ndr()
2615
2616         tsf1_list = [ndr32]
2617         ctx1 = dcerpc.ctx_list()
2618         ctx1.context_id = 1
2619         ctx1.num_transfer_syntaxes = len(tsf1_list)
2620         ctx1.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
2621         ctx1.transfer_syntaxes = tsf1_list
2622         ctx_list = [ctx1]
2623
2624         c = self.get_anon_creds()
2625         g = gensec.Security.start_client(self.settings)
2626         g.set_credentials(c)
2627         g.want_feature(gensec.FEATURE_DCE_STYLE)
2628         auth_type = dcerpc.DCERPC_AUTH_TYPE_SPNEGO
2629         auth_level = dcerpc.DCERPC_AUTH_LEVEL_CONNECT
2630         auth_context_id = 2
2631         g.start_mech_by_authtype(auth_type, auth_level)
2632         from_server = ""
2633         (finished, to_server) = g.update(from_server)
2634         self.assertFalse(finished)
2635
2636         auth_info = self.generate_auth(auth_type=auth_type,
2637                                        auth_level=auth_level,
2638                                        auth_context_id=auth_context_id,
2639                                        auth_blob=to_server)
2640
2641         req = self.generate_bind(call_id=0,
2642                                  ctx_list=ctx_list,
2643                                  auth_info=auth_info)
2644
2645         self.send_pdu(req)
2646         rep = self.recv_pdu()
2647         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id)
2648         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
2649         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
2650         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
2651         self.assertEquals(rep.u.secondary_address_size, 4)
2652         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)
2653         self.assertEquals(len(rep.u._pad1), 2)
2654         self.assertEquals(rep.u._pad1, '\0' * 2)
2655         self.assertEquals(rep.u.num_results, 1)
2656         self.assertEquals(rep.u.ctx_list[0].result,
2657                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
2658         self.assertEquals(rep.u.ctx_list[0].reason,
2659                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
2660         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
2661         self.assertNotEquals(len(rep.u.auth_info), 0)
2662         a = self.parse_auth(rep.u.auth_info)
2663
2664         from_server = a.credentials
2665         (finished, to_server) = g.update(from_server)
2666         self.assertFalse(finished)
2667
2668         auth_info = self.generate_auth(auth_type=auth_type,
2669                                        auth_level=auth_level,
2670                                        auth_context_id=auth_context_id,
2671                                        auth_blob=to_server)
2672
2673         req = self.generate_alter(call_id=0,
2674                                   ctx_list=ctx_list,
2675                                   assoc_group_id=rep.u.assoc_group_id,
2676                                   auth_info=auth_info)
2677
2678         self.send_pdu(req)
2679         rep = self.recv_pdu()
2680         self.verify_pdu(rep, dcerpc.DCERPC_PKT_ALTER_RESP, req.call_id)
2681         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
2682         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
2683         self.assertEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
2684         self.assertEquals(rep.u.secondary_address_size, 0)
2685         self.assertEquals(len(rep.u._pad1), 2)
2686         # Windows sends garbage
2687         #self.assertEquals(rep.u._pad1, '\0' * 2)
2688         self.assertEquals(rep.u.num_results, 1)
2689         self.assertEquals(rep.u.ctx_list[0].result,
2690                           dcerpc.DCERPC_BIND_ACK_RESULT_ACCEPTANCE)
2691         self.assertEquals(rep.u.ctx_list[0].reason,
2692                           dcerpc.DCERPC_BIND_ACK_REASON_NOT_SPECIFIED)
2693         self.assertNDRSyntaxEquals(rep.u.ctx_list[0].syntax, ndr32)
2694         self.assertNotEquals(len(rep.u.auth_info), 0)
2695         a = self.parse_auth(rep.u.auth_info)
2696
2697         from_server = a.credentials
2698         (finished, to_server) = g.update(from_server)
2699         self.assertTrue(finished)
2700
2701         # And now try a request without auth_info
2702         req = self.generate_request(call_id = 2,
2703                                     context_id=ctx1.context_id,
2704                                     opnum=0,
2705                                     stub="")
2706         self.send_pdu(req)
2707         rep = self.recv_pdu()
2708         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
2709                         auth_length=0)
2710         self.assertNotEquals(rep.u.alloc_hint, 0)
2711         self.assertEquals(rep.u.context_id, req.u.context_id & 0xff)
2712         self.assertEquals(rep.u.cancel_count, 0)
2713         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
2714
2715         # Now a request with auth_info DCERPC_AUTH_LEVEL_CONNECT
2716         auth_info = self.generate_auth(auth_type=auth_type,
2717                                        auth_level=auth_level,
2718                                        auth_context_id=auth_context_id,
2719                                        auth_blob="\x01" +"\x00" *15)
2720         req = self.generate_request(call_id = 3,
2721                                     context_id=ctx1.context_id,
2722                                     opnum=0,
2723                                     stub="",
2724                                     auth_info=auth_info)
2725         self.send_pdu(req)
2726         rep = self.recv_pdu()
2727         # We don't get an auth_info back
2728         self.verify_pdu(rep, dcerpc.DCERPC_PKT_RESPONSE, req.call_id,
2729                         auth_length=0)
2730         self.assertNotEquals(rep.u.alloc_hint, 0)
2731         self.assertEquals(rep.u.context_id, req.u.context_id & 0xff)
2732         self.assertEquals(rep.u.cancel_count, 0)
2733         self.assertGreaterEqual(len(rep.u.stub_and_verifier), rep.u.alloc_hint)
2734
2735         # Now a request with auth_info DCERPC_AUTH_LEVEL_INTEGRITY
2736         auth_info = self.generate_auth(auth_type=auth_type,
2737                                        auth_level=dcerpc.DCERPC_AUTH_LEVEL_INTEGRITY,
2738                                        auth_context_id=auth_context_id,
2739                                        auth_blob="\x01" +"\x00" * 15)
2740         req = self.generate_request(call_id = 4,
2741                                     context_id=ctx1.context_id,
2742                                     opnum=0,
2743                                     stub="",
2744                                     auth_info=auth_info)
2745         self.send_pdu(req)
2746         rep = self.recv_pdu()
2747         # We get a fault back
2748         self.verify_pdu(rep, dcerpc.DCERPC_PKT_FAULT, req.call_id,
2749                         auth_length=0)
2750         self.assertNotEquals(rep.u.alloc_hint, 0)
2751         self.assertEquals(rep.u.context_id, req.u.context_id)
2752         self.assertEquals(rep.u.cancel_count, 0)
2753         self.assertEquals(rep.u.flags, 0)
2754         self.assertEquals(rep.u.status, dcerpc.DCERPC_FAULT_ACCESS_DENIED)
2755         self.assertEquals(rep.u.reserved, 0)
2756         self.assertEquals(len(rep.u.error_and_verifier), 0)
2757
2758         # wait for a disconnect
2759         rep = self.recv_pdu()
2760         self.assertIsNone(rep)
2761         self.assertNotConnected()
2762
2763     def test_spnego_integrity_request(self):
2764         ndr32 = base.transfer_syntax_ndr()
2765
2766         tsf1_list = [ndr32]
2767         ctx1 = dcerpc.ctx_list()
2768         ctx1.context_id = 1
2769         ctx1.num_transfer_syntaxes = len(tsf1_list)
2770         ctx1.abstract_syntax = samba.dcerpc.mgmt.abstract_syntax()
2771         ctx1.transfer_syntaxes = tsf1_list
2772         ctx_list = [ctx1]
2773
2774         c = self.get_anon_creds()
2775         g = gensec.Security.start_client(self.settings)
2776         g.set_credentials(c)
2777         g.want_feature(gensec.FEATURE_DCE_STYLE)
2778         auth_type = dcerpc.DCERPC_AUTH_TYPE_SPNEGO
2779         auth_level = dcerpc.DCERPC_AUTH_LEVEL_INTEGRITY
2780         auth_context_id = 2
2781         g.start_mech_by_authtype(auth_type, auth_level)
2782         from_server = ""
2783         (finished, to_server) = g.update(from_server)
2784         self.assertFalse(finished)
2785
2786         auth_info = self.generate_auth(auth_type=auth_type,
2787                                        auth_level=auth_level,
2788                                        auth_context_id=auth_context_id,
2789                                        auth_blob=to_server)
2790
2791         req = self.generate_bind(call_id=0,
2792                                  ctx_list=ctx_list,
2793                                  auth_info=auth_info)
2794
2795         self.send_pdu(req)
2796         rep = self.recv_pdu()
2797         self.verify_pdu(rep, dcerpc.DCERPC_PKT_BIND_ACK, req.call_id)
2798         self.assertEquals(rep.u.max_xmit_frag, req.u.max_xmit_frag)
2799         self.assertEquals(rep.u.max_recv_frag, req.u.max_recv_frag)
2800         self.assertNotEquals(rep.u.assoc_group_id, req.u.assoc_group_id)
2801         self.assertEquals(rep.u.secondary_address_size, 4)
2802         self.assertEquals(rep.u.secondary_address, "%d" % self.tcp_port)