RIP BOOL. Convert BOOL -> bool. I found a few interesting
[tprouty/samba.git] / source / rpc_server / srv_pipe.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Almost completely rewritten by (C) Jeremy Allison 2005.
5  *  
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /*  this module apparently provides an implementation of DCE/RPC over a
21  *  named pipe (IPC$ connection using SMBtrans).  details of DCE/RPC
22  *  documentation are available (in on-line form) from the X-Open group.
23  *
24  *  this module should provide a level of abstraction between SMB
25  *  and DCE/RPC, while minimising the amount of mallocs, unnecessary
26  *  data copies, and network traffic.
27  *
28  */
29
30 #include "includes.h"
31
32 extern struct pipe_id_info pipe_names[];
33 extern struct current_user current_user;
34
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_RPC_SRV
37
38 static void free_pipe_ntlmssp_auth_data(struct pipe_auth_data *auth)
39 {
40         AUTH_NTLMSSP_STATE *a = auth->a_u.auth_ntlmssp_state;
41
42         if (a) {
43                 auth_ntlmssp_end(&a);
44         }
45         auth->a_u.auth_ntlmssp_state = NULL;
46 }
47
48 static DATA_BLOB generic_session_key(void)
49 {
50         return data_blob("SystemLibraryDTC", 16);
51 }
52
53 /*******************************************************************
54  Generate the next PDU to be returned from the data in p->rdata. 
55  Handle NTLMSSP.
56  ********************************************************************/
57
58 static bool create_next_pdu_ntlmssp(pipes_struct *p)
59 {
60         RPC_HDR_RESP hdr_resp;
61         uint32 ss_padding_len = 0;
62         uint32 data_space_available;
63         uint32 data_len_left;
64         uint32 data_len;
65         prs_struct outgoing_pdu;
66         NTSTATUS status;
67         DATA_BLOB auth_blob;
68         RPC_HDR_AUTH auth_info;
69         uint8 auth_type, auth_level;
70         AUTH_NTLMSSP_STATE *a = p->auth.a_u.auth_ntlmssp_state;
71
72         /*
73          * If we're in the fault state, keep returning fault PDU's until
74          * the pipe gets closed. JRA.
75          */
76
77         if(p->fault_state) {
78                 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
79                 return True;
80         }
81
82         memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
83
84         /* Change the incoming request header to a response. */
85         p->hdr.pkt_type = RPC_RESPONSE;
86
87         /* Set up rpc header flags. */
88         if (p->out_data.data_sent_length == 0) {
89                 p->hdr.flags = RPC_FLG_FIRST;
90         } else {
91                 p->hdr.flags = 0;
92         }
93
94         /*
95          * Work out how much we can fit in a single PDU.
96          */
97
98         data_len_left = prs_offset(&p->out_data.rdata) - p->out_data.data_sent_length;
99
100         /*
101          * Ensure there really is data left to send.
102          */
103
104         if(!data_len_left) {
105                 DEBUG(0,("create_next_pdu_ntlmssp: no data left to send !\n"));
106                 return False;
107         }
108
109         data_space_available = sizeof(p->out_data.current_pdu) - RPC_HEADER_LEN - RPC_HDR_RESP_LEN -
110                                         RPC_HDR_AUTH_LEN - NTLMSSP_SIG_SIZE;
111
112         /*
113          * The amount we send is the minimum of the available
114          * space and the amount left to send.
115          */
116
117         data_len = MIN(data_len_left, data_space_available);
118
119         /*
120          * Set up the alloc hint. This should be the data left to
121          * send.
122          */
123
124         hdr_resp.alloc_hint = data_len_left;
125
126         /*
127          * Work out if this PDU will be the last.
128          */
129
130         if(p->out_data.data_sent_length + data_len >= prs_offset(&p->out_data.rdata)) {
131                 p->hdr.flags |= RPC_FLG_LAST;
132                 if (data_len_left % 8) {
133                         ss_padding_len = 8 - (data_len_left % 8);
134                         DEBUG(10,("create_next_pdu_ntlmssp: adding sign/seal padding of %u\n",
135                                 ss_padding_len ));
136                 }
137         }
138
139         /*
140          * Set up the header lengths.
141          */
142
143         p->hdr.frag_len = RPC_HEADER_LEN + RPC_HDR_RESP_LEN +
144                         data_len + ss_padding_len +
145                         RPC_HDR_AUTH_LEN + NTLMSSP_SIG_SIZE;
146         p->hdr.auth_len = NTLMSSP_SIG_SIZE;
147
148
149         /*
150          * Init the parse struct to point at the outgoing
151          * data.
152          */
153
154         prs_init( &outgoing_pdu, 0, p->mem_ctx, MARSHALL);
155         prs_give_memory( &outgoing_pdu, (char *)p->out_data.current_pdu, sizeof(p->out_data.current_pdu), False);
156
157         /* Store the header in the data stream. */
158         if(!smb_io_rpc_hdr("hdr", &p->hdr, &outgoing_pdu, 0)) {
159                 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR.\n"));
160                 prs_mem_free(&outgoing_pdu);
161                 return False;
162         }
163
164         if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &outgoing_pdu, 0)) {
165                 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR_RESP.\n"));
166                 prs_mem_free(&outgoing_pdu);
167                 return False;
168         }
169
170         /* Copy the data into the PDU. */
171
172         if(!prs_append_some_prs_data(&outgoing_pdu, &p->out_data.rdata, p->out_data.data_sent_length, data_len)) {
173                 DEBUG(0,("create_next_pdu_ntlmssp: failed to copy %u bytes of data.\n", (unsigned int)data_len));
174                 prs_mem_free(&outgoing_pdu);
175                 return False;
176         }
177
178         /* Copy the sign/seal padding data. */
179         if (ss_padding_len) {
180                 char pad[8];
181
182                 memset(pad, '\0', 8);
183                 if (!prs_copy_data_in(&outgoing_pdu, pad, ss_padding_len)) {
184                         DEBUG(0,("create_next_pdu_ntlmssp: failed to add %u bytes of pad data.\n",
185                                         (unsigned int)ss_padding_len));
186                         prs_mem_free(&outgoing_pdu);
187                         return False;
188                 }
189         }
190
191
192         /* Now write out the auth header and null blob. */
193         if (p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP) {
194                 auth_type = RPC_NTLMSSP_AUTH_TYPE;
195         } else {
196                 auth_type = RPC_SPNEGO_AUTH_TYPE;
197         }
198         if (p->auth.auth_level == PIPE_AUTH_LEVEL_PRIVACY) {
199                 auth_level = RPC_AUTH_LEVEL_PRIVACY;
200         } else {
201                 auth_level = RPC_AUTH_LEVEL_INTEGRITY;
202         }
203
204         init_rpc_hdr_auth(&auth_info, auth_type, auth_level, ss_padding_len, 1 /* context id. */);
205         if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info, &outgoing_pdu, 0)) {
206                 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR_AUTH.\n"));
207                 prs_mem_free(&outgoing_pdu);
208                 return False;
209         }
210
211         /* Generate the sign blob. */
212
213         switch (p->auth.auth_level) {
214                 case PIPE_AUTH_LEVEL_PRIVACY:
215                         /* Data portion is encrypted. */
216                         status = ntlmssp_seal_packet(a->ntlmssp_state,
217                                                         (unsigned char *)prs_data_p(&outgoing_pdu) + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
218                                                         data_len + ss_padding_len,
219                                                         (unsigned char *)prs_data_p(&outgoing_pdu),
220                                                         (size_t)prs_offset(&outgoing_pdu),
221                                                         &auth_blob);
222                         if (!NT_STATUS_IS_OK(status)) {
223                                 data_blob_free(&auth_blob);
224                                 prs_mem_free(&outgoing_pdu);
225                                 return False;
226                         }
227                         break;
228                 case PIPE_AUTH_LEVEL_INTEGRITY:
229                         /* Data is signed. */
230                         status = ntlmssp_sign_packet(a->ntlmssp_state,
231                                                         (unsigned char *)prs_data_p(&outgoing_pdu) + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
232                                                         data_len + ss_padding_len,
233                                                         (unsigned char *)prs_data_p(&outgoing_pdu),
234                                                         (size_t)prs_offset(&outgoing_pdu),
235                                                         &auth_blob);
236                         if (!NT_STATUS_IS_OK(status)) {
237                                 data_blob_free(&auth_blob);
238                                 prs_mem_free(&outgoing_pdu);
239                                 return False;
240                         }
241                         break;
242                 default:
243                         prs_mem_free(&outgoing_pdu);
244                         return False;
245         }
246
247         /* Append the auth blob. */
248         if (!prs_copy_data_in(&outgoing_pdu, (char *)auth_blob.data, NTLMSSP_SIG_SIZE)) {
249                 DEBUG(0,("create_next_pdu_ntlmssp: failed to add %u bytes auth blob.\n",
250                                 (unsigned int)NTLMSSP_SIG_SIZE));
251                 data_blob_free(&auth_blob);
252                 prs_mem_free(&outgoing_pdu);
253                 return False;
254         }
255
256         data_blob_free(&auth_blob);
257
258         /*
259          * Setup the counts for this PDU.
260          */
261
262         p->out_data.data_sent_length += data_len;
263         p->out_data.current_pdu_len = p->hdr.frag_len;
264         p->out_data.current_pdu_sent = 0;
265
266         prs_mem_free(&outgoing_pdu);
267         return True;
268 }
269
270 /*******************************************************************
271  Generate the next PDU to be returned from the data in p->rdata. 
272  Return an schannel authenticated fragment.
273  ********************************************************************/
274
275 static bool create_next_pdu_schannel(pipes_struct *p)
276 {
277         RPC_HDR_RESP hdr_resp;
278         uint32 ss_padding_len = 0;
279         uint32 data_len;
280         uint32 data_space_available;
281         uint32 data_len_left;
282         prs_struct outgoing_pdu;
283         uint32 data_pos;
284
285         /*
286          * If we're in the fault state, keep returning fault PDU's until
287          * the pipe gets closed. JRA.
288          */
289
290         if(p->fault_state) {
291                 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
292                 return True;
293         }
294
295         memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
296
297         /* Change the incoming request header to a response. */
298         p->hdr.pkt_type = RPC_RESPONSE;
299
300         /* Set up rpc header flags. */
301         if (p->out_data.data_sent_length == 0) {
302                 p->hdr.flags = RPC_FLG_FIRST;
303         } else {
304                 p->hdr.flags = 0;
305         }
306
307         /*
308          * Work out how much we can fit in a single PDU.
309          */
310
311         data_len_left = prs_offset(&p->out_data.rdata) - p->out_data.data_sent_length;
312
313         /*
314          * Ensure there really is data left to send.
315          */
316
317         if(!data_len_left) {
318                 DEBUG(0,("create_next_pdu_schannel: no data left to send !\n"));
319                 return False;
320         }
321
322         data_space_available = sizeof(p->out_data.current_pdu) - RPC_HEADER_LEN - RPC_HDR_RESP_LEN -
323                                         RPC_HDR_AUTH_LEN - RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
324
325         /*
326          * The amount we send is the minimum of the available
327          * space and the amount left to send.
328          */
329
330         data_len = MIN(data_len_left, data_space_available);
331
332         /*
333          * Set up the alloc hint. This should be the data left to
334          * send.
335          */
336
337         hdr_resp.alloc_hint = data_len_left;
338
339         /*
340          * Work out if this PDU will be the last.
341          */
342
343         if(p->out_data.data_sent_length + data_len >= prs_offset(&p->out_data.rdata)) {
344                 p->hdr.flags |= RPC_FLG_LAST;
345                 if (data_len_left % 8) {
346                         ss_padding_len = 8 - (data_len_left % 8);
347                         DEBUG(10,("create_next_pdu_schannel: adding sign/seal padding of %u\n",
348                                 ss_padding_len ));
349                 }
350         }
351
352         p->hdr.frag_len = RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len + ss_padding_len +
353                                 RPC_HDR_AUTH_LEN + RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
354         p->hdr.auth_len = RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
355
356         /*
357          * Init the parse struct to point at the outgoing
358          * data.
359          */
360
361         prs_init( &outgoing_pdu, 0, p->mem_ctx, MARSHALL);
362         prs_give_memory( &outgoing_pdu, (char *)p->out_data.current_pdu, sizeof(p->out_data.current_pdu), False);
363
364         /* Store the header in the data stream. */
365         if(!smb_io_rpc_hdr("hdr", &p->hdr, &outgoing_pdu, 0)) {
366                 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR.\n"));
367                 prs_mem_free(&outgoing_pdu);
368                 return False;
369         }
370
371         if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &outgoing_pdu, 0)) {
372                 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR_RESP.\n"));
373                 prs_mem_free(&outgoing_pdu);
374                 return False;
375         }
376
377         /* Store the current offset. */
378         data_pos = prs_offset(&outgoing_pdu);
379
380         /* Copy the data into the PDU. */
381
382         if(!prs_append_some_prs_data(&outgoing_pdu, &p->out_data.rdata, p->out_data.data_sent_length, data_len)) {
383                 DEBUG(0,("create_next_pdu_schannel: failed to copy %u bytes of data.\n", (unsigned int)data_len));
384                 prs_mem_free(&outgoing_pdu);
385                 return False;
386         }
387
388         /* Copy the sign/seal padding data. */
389         if (ss_padding_len) {
390                 char pad[8];
391                 memset(pad, '\0', 8);
392                 if (!prs_copy_data_in(&outgoing_pdu, pad, ss_padding_len)) {
393                         DEBUG(0,("create_next_pdu_schannel: failed to add %u bytes of pad data.\n", (unsigned int)ss_padding_len));
394                         prs_mem_free(&outgoing_pdu);
395                         return False;
396                 }
397         }
398
399         {
400                 /*
401                  * Schannel processing.
402                  */
403                 char *data;
404                 RPC_HDR_AUTH auth_info;
405                 RPC_AUTH_SCHANNEL_CHK verf;
406
407                 data = prs_data_p(&outgoing_pdu) + data_pos;
408                 /* Check it's the type of reply we were expecting to decode */
409
410                 init_rpc_hdr_auth(&auth_info,
411                                 RPC_SCHANNEL_AUTH_TYPE,
412                                 p->auth.auth_level == PIPE_AUTH_LEVEL_PRIVACY ?
413                                         RPC_AUTH_LEVEL_PRIVACY : RPC_AUTH_LEVEL_INTEGRITY,
414                                 ss_padding_len, 1);
415
416                 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info, &outgoing_pdu, 0)) {
417                         DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR_AUTH.\n"));
418                         prs_mem_free(&outgoing_pdu);
419                         return False;
420                 }
421
422                 schannel_encode(p->auth.a_u.schannel_auth, 
423                               p->auth.auth_level,
424                               SENDER_IS_ACCEPTOR,
425                               &verf, data, data_len + ss_padding_len);
426
427                 if (!smb_io_rpc_auth_schannel_chk("", RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN, 
428                                 &verf, &outgoing_pdu, 0)) {
429                         prs_mem_free(&outgoing_pdu);
430                         return False;
431                 }
432
433                 p->auth.a_u.schannel_auth->seq_num++;
434         }
435
436         /*
437          * Setup the counts for this PDU.
438          */
439
440         p->out_data.data_sent_length += data_len;
441         p->out_data.current_pdu_len = p->hdr.frag_len;
442         p->out_data.current_pdu_sent = 0;
443
444         prs_mem_free(&outgoing_pdu);
445         return True;
446 }
447
448 /*******************************************************************
449  Generate the next PDU to be returned from the data in p->rdata. 
450  No authentication done.
451 ********************************************************************/
452
453 static bool create_next_pdu_noauth(pipes_struct *p)
454 {
455         RPC_HDR_RESP hdr_resp;
456         uint32 data_len;
457         uint32 data_space_available;
458         uint32 data_len_left;
459         prs_struct outgoing_pdu;
460
461         /*
462          * If we're in the fault state, keep returning fault PDU's until
463          * the pipe gets closed. JRA.
464          */
465
466         if(p->fault_state) {
467                 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
468                 return True;
469         }
470
471         memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
472
473         /* Change the incoming request header to a response. */
474         p->hdr.pkt_type = RPC_RESPONSE;
475
476         /* Set up rpc header flags. */
477         if (p->out_data.data_sent_length == 0) {
478                 p->hdr.flags = RPC_FLG_FIRST;
479         } else {
480                 p->hdr.flags = 0;
481         }
482
483         /*
484          * Work out how much we can fit in a single PDU.
485          */
486
487         data_len_left = prs_offset(&p->out_data.rdata) - p->out_data.data_sent_length;
488
489         /*
490          * Ensure there really is data left to send.
491          */
492
493         if(!data_len_left) {
494                 DEBUG(0,("create_next_pdu_noath: no data left to send !\n"));
495                 return False;
496         }
497
498         data_space_available = sizeof(p->out_data.current_pdu) - RPC_HEADER_LEN - RPC_HDR_RESP_LEN;
499
500         /*
501          * The amount we send is the minimum of the available
502          * space and the amount left to send.
503          */
504
505         data_len = MIN(data_len_left, data_space_available);
506
507         /*
508          * Set up the alloc hint. This should be the data left to
509          * send.
510          */
511
512         hdr_resp.alloc_hint = data_len_left;
513
514         /*
515          * Work out if this PDU will be the last.
516          */
517
518         if(p->out_data.data_sent_length + data_len >= prs_offset(&p->out_data.rdata)) {
519                 p->hdr.flags |= RPC_FLG_LAST;
520         }
521
522         /*
523          * Set up the header lengths.
524          */
525
526         p->hdr.frag_len = RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len;
527         p->hdr.auth_len = 0;
528
529         /*
530          * Init the parse struct to point at the outgoing
531          * data.
532          */
533
534         prs_init( &outgoing_pdu, 0, p->mem_ctx, MARSHALL);
535         prs_give_memory( &outgoing_pdu, (char *)p->out_data.current_pdu, sizeof(p->out_data.current_pdu), False);
536
537         /* Store the header in the data stream. */
538         if(!smb_io_rpc_hdr("hdr", &p->hdr, &outgoing_pdu, 0)) {
539                 DEBUG(0,("create_next_pdu_noath: failed to marshall RPC_HDR.\n"));
540                 prs_mem_free(&outgoing_pdu);
541                 return False;
542         }
543
544         if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &outgoing_pdu, 0)) {
545                 DEBUG(0,("create_next_pdu_noath: failed to marshall RPC_HDR_RESP.\n"));
546                 prs_mem_free(&outgoing_pdu);
547                 return False;
548         }
549
550         /* Copy the data into the PDU. */
551
552         if(!prs_append_some_prs_data(&outgoing_pdu, &p->out_data.rdata, p->out_data.data_sent_length, data_len)) {
553                 DEBUG(0,("create_next_pdu_noauth: failed to copy %u bytes of data.\n", (unsigned int)data_len));
554                 prs_mem_free(&outgoing_pdu);
555                 return False;
556         }
557
558         /*
559          * Setup the counts for this PDU.
560          */
561
562         p->out_data.data_sent_length += data_len;
563         p->out_data.current_pdu_len = p->hdr.frag_len;
564         p->out_data.current_pdu_sent = 0;
565
566         prs_mem_free(&outgoing_pdu);
567         return True;
568 }
569
570 /*******************************************************************
571  Generate the next PDU to be returned from the data in p->rdata. 
572 ********************************************************************/
573
574 bool create_next_pdu(pipes_struct *p)
575 {
576         switch(p->auth.auth_level) {
577                 case PIPE_AUTH_LEVEL_NONE:
578                 case PIPE_AUTH_LEVEL_CONNECT:
579                         /* This is incorrect for auth level connect. Fixme. JRA */
580                         return create_next_pdu_noauth(p);
581                 
582                 default:
583                         switch(p->auth.auth_type) {
584                                 case PIPE_AUTH_TYPE_NTLMSSP:
585                                 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
586                                         return create_next_pdu_ntlmssp(p);
587                                 case PIPE_AUTH_TYPE_SCHANNEL:
588                                         return create_next_pdu_schannel(p);
589                                 default:
590                                         break;
591                         }
592         }
593
594         DEBUG(0,("create_next_pdu: invalid internal auth level %u / type %u",
595                         (unsigned int)p->auth.auth_level,
596                         (unsigned int)p->auth.auth_type));
597         return False;
598 }
599
600 /*******************************************************************
601  Process an NTLMSSP authentication response.
602  If this function succeeds, the user has been authenticated
603  and their domain, name and calling workstation stored in
604  the pipe struct.
605 *******************************************************************/
606
607 static bool pipe_ntlmssp_verify_final(pipes_struct *p, DATA_BLOB *p_resp_blob)
608 {
609         DATA_BLOB reply;
610         NTSTATUS status;
611         AUTH_NTLMSSP_STATE *a = p->auth.a_u.auth_ntlmssp_state;
612
613         DEBUG(5,("pipe_ntlmssp_verify_final: pipe %s checking user details\n", p->name));
614
615         ZERO_STRUCT(reply);
616
617         memset(p->user_name, '\0', sizeof(p->user_name));
618         memset(p->pipe_user_name, '\0', sizeof(p->pipe_user_name));
619         memset(p->domain, '\0', sizeof(p->domain));
620         memset(p->wks, '\0', sizeof(p->wks));
621
622         /* Set up for non-authenticated user. */
623         TALLOC_FREE(p->pipe_user.nt_user_token);
624         p->pipe_user.ut.ngroups = 0;
625         SAFE_FREE( p->pipe_user.ut.groups);
626
627         /* this has to be done as root in order to verify the password */
628         become_root();
629         status = auth_ntlmssp_update(a, *p_resp_blob, &reply);
630         unbecome_root();
631
632         /* Don't generate a reply. */
633         data_blob_free(&reply);
634
635         if (!NT_STATUS_IS_OK(status)) {
636                 return False;
637         }
638
639         /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
640            ensure the underlying NTLMSSP flags are also set. If not we should
641            refuse the bind. */
642
643         if (p->auth.auth_level == PIPE_AUTH_LEVEL_INTEGRITY) {
644                 if (!(a->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
645                         DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet integrity requested "
646                                 "but client declined signing.\n",
647                                         p->name ));
648                         return False;
649                 }
650         }
651         if (p->auth.auth_level == PIPE_AUTH_LEVEL_PRIVACY) {
652                 if (!(a->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
653                         DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet privacy requested "
654                                 "but client declined sealing.\n",
655                                         p->name ));
656                         return False;
657                 }
658         }
659         
660         fstrcpy(p->user_name, a->ntlmssp_state->user);
661         fstrcpy(p->pipe_user_name, a->server_info->unix_name);
662         fstrcpy(p->domain, a->ntlmssp_state->domain);
663         fstrcpy(p->wks, a->ntlmssp_state->workstation);
664
665         DEBUG(5,("pipe_ntlmssp_verify_final: OK: user: %s domain: %s workstation: %s\n",
666                 p->user_name, p->domain, p->wks));
667
668         /*
669          * Store the UNIX credential data (uid/gid pair) in the pipe structure.
670          */
671
672         p->pipe_user.ut.uid = a->server_info->uid;
673         p->pipe_user.ut.gid = a->server_info->gid;
674         
675         /*
676          * We're an authenticated bind over smbd, so the session key needs to
677          * be set to "SystemLibraryDTC". Weird, but this is what Windows
678          * does. See the RPC-SAMBA3SESSIONKEY.
679          */
680
681         data_blob_free(&p->session_key);
682         p->session_key = generic_session_key();
683         if (!p->session_key.data) {
684                 return False;
685         }
686
687         p->pipe_user.ut.ngroups = a->server_info->n_groups;
688         if (p->pipe_user.ut.ngroups) {
689                 if (!(p->pipe_user.ut.groups = (gid_t *)memdup(a->server_info->groups,
690                                                 sizeof(gid_t) * p->pipe_user.ut.ngroups))) {
691                         DEBUG(0,("failed to memdup group list to p->pipe_user.groups\n"));
692                         return False;
693                 }
694         }
695
696         if (a->server_info->ptok) {
697                 p->pipe_user.nt_user_token =
698                         dup_nt_token(NULL, a->server_info->ptok);
699         } else {
700                 DEBUG(1,("Error: Authmodule failed to provide nt_user_token\n"));
701                 p->pipe_user.nt_user_token = NULL;
702                 return False;
703         }
704
705         return True;
706 }
707
708 /*******************************************************************
709  The switch table for the pipe names and the functions to handle them.
710 *******************************************************************/
711
712 struct rpc_table {
713         struct {
714                 const char *clnt;
715                 const char *srv;
716         } pipe;
717         struct api_struct *cmds;
718         int n_cmds;
719 };
720
721 static struct rpc_table *rpc_lookup;
722 static int rpc_lookup_size;
723
724 /*******************************************************************
725  This is the "stage3" NTLMSSP response after a bind request and reply.
726 *******************************************************************/
727
728 bool api_pipe_bind_auth3(pipes_struct *p, prs_struct *rpc_in_p)
729 {
730         RPC_HDR_AUTH auth_info;
731         uint32 pad;
732         DATA_BLOB blob;
733
734         ZERO_STRUCT(blob);
735
736         DEBUG(5,("api_pipe_bind_auth3: decode request. %d\n", __LINE__));
737
738         if (p->hdr.auth_len == 0) {
739                 DEBUG(0,("api_pipe_bind_auth3: No auth field sent !\n"));
740                 goto err;
741         }
742
743         /* 4 bytes padding. */
744         if (!prs_uint32("pad", rpc_in_p, 0, &pad)) {
745                 DEBUG(0,("api_pipe_bind_auth3: unmarshall of 4 byte pad failed.\n"));
746                 goto err;
747         }
748
749         /*
750          * Decode the authentication verifier response.
751          */
752
753         if(!smb_io_rpc_hdr_auth("", &auth_info, rpc_in_p, 0)) {
754                 DEBUG(0,("api_pipe_bind_auth3: unmarshall of RPC_HDR_AUTH failed.\n"));
755                 goto err;
756         }
757
758         if (auth_info.auth_type != RPC_NTLMSSP_AUTH_TYPE) {
759                 DEBUG(0,("api_pipe_bind_auth3: incorrect auth type (%u).\n",
760                         (unsigned int)auth_info.auth_type ));
761                 return False;
762         }
763
764         blob = data_blob(NULL,p->hdr.auth_len);
765
766         if (!prs_copy_data_out((char *)blob.data, rpc_in_p, p->hdr.auth_len)) {
767                 DEBUG(0,("api_pipe_bind_auth3: Failed to pull %u bytes - the response blob.\n",
768                         (unsigned int)p->hdr.auth_len ));
769                 goto err;
770         }
771
772         /*
773          * The following call actually checks the challenge/response data.
774          * for correctness against the given DOMAIN\user name.
775          */
776         
777         if (!pipe_ntlmssp_verify_final(p, &blob)) {
778                 goto err;
779         }
780
781         data_blob_free(&blob);
782
783         p->pipe_bound = True;
784
785         return True;
786
787  err:
788
789         data_blob_free(&blob);
790         free_pipe_ntlmssp_auth_data(&p->auth);
791         p->auth.a_u.auth_ntlmssp_state = NULL;
792
793         return False;
794 }
795
796 /*******************************************************************
797  Marshall a bind_nak pdu.
798 *******************************************************************/
799
800 static bool setup_bind_nak(pipes_struct *p)
801 {
802         prs_struct outgoing_rpc;
803         RPC_HDR nak_hdr;
804         uint16 zero = 0;
805
806         /* Free any memory in the current return data buffer. */
807         prs_mem_free(&p->out_data.rdata);
808
809         /*
810          * Marshall directly into the outgoing PDU space. We
811          * must do this as we need to set to the bind response
812          * header and are never sending more than one PDU here.
813          */
814
815         prs_init( &outgoing_rpc, 0, p->mem_ctx, MARSHALL);
816         prs_give_memory( &outgoing_rpc, (char *)p->out_data.current_pdu, sizeof(p->out_data.current_pdu), False);
817
818         /*
819          * Initialize a bind_nak header.
820          */
821
822         init_rpc_hdr(&nak_hdr, RPC_BINDNACK, RPC_FLG_FIRST | RPC_FLG_LAST,
823                 p->hdr.call_id, RPC_HEADER_LEN + sizeof(uint16), 0);
824
825         /*
826          * Marshall the header into the outgoing PDU.
827          */
828
829         if(!smb_io_rpc_hdr("", &nak_hdr, &outgoing_rpc, 0)) {
830                 DEBUG(0,("setup_bind_nak: marshalling of RPC_HDR failed.\n"));
831                 prs_mem_free(&outgoing_rpc);
832                 return False;
833         }
834
835         /*
836          * Now add the reject reason.
837          */
838
839         if(!prs_uint16("reject code", &outgoing_rpc, 0, &zero)) {
840                 prs_mem_free(&outgoing_rpc);
841                 return False;
842         }
843
844         p->out_data.data_sent_length = 0;
845         p->out_data.current_pdu_len = prs_offset(&outgoing_rpc);
846         p->out_data.current_pdu_sent = 0;
847
848         if (p->auth.auth_data_free_func) {
849                 (*p->auth.auth_data_free_func)(&p->auth);
850         }
851         p->auth.auth_level = PIPE_AUTH_LEVEL_NONE;
852         p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
853         p->pipe_bound = False;
854
855         return True;
856 }
857
858 /*******************************************************************
859  Marshall a fault pdu.
860 *******************************************************************/
861
862 bool setup_fault_pdu(pipes_struct *p, NTSTATUS status)
863 {
864         prs_struct outgoing_pdu;
865         RPC_HDR fault_hdr;
866         RPC_HDR_RESP hdr_resp;
867         RPC_HDR_FAULT fault_resp;
868
869         /* Free any memory in the current return data buffer. */
870         prs_mem_free(&p->out_data.rdata);
871
872         /*
873          * Marshall directly into the outgoing PDU space. We
874          * must do this as we need to set to the bind response
875          * header and are never sending more than one PDU here.
876          */
877
878         prs_init( &outgoing_pdu, 0, p->mem_ctx, MARSHALL);
879         prs_give_memory( &outgoing_pdu, (char *)p->out_data.current_pdu, sizeof(p->out_data.current_pdu), False);
880
881         /*
882          * Initialize a fault header.
883          */
884
885         init_rpc_hdr(&fault_hdr, RPC_FAULT, RPC_FLG_FIRST | RPC_FLG_LAST | RPC_FLG_NOCALL,
886             p->hdr.call_id, RPC_HEADER_LEN + RPC_HDR_RESP_LEN + RPC_HDR_FAULT_LEN, 0);
887
888         /*
889          * Initialize the HDR_RESP and FAULT parts of the PDU.
890          */
891
892         memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
893
894         fault_resp.status = status;
895         fault_resp.reserved = 0;
896
897         /*
898          * Marshall the header into the outgoing PDU.
899          */
900
901         if(!smb_io_rpc_hdr("", &fault_hdr, &outgoing_pdu, 0)) {
902                 DEBUG(0,("setup_fault_pdu: marshalling of RPC_HDR failed.\n"));
903                 prs_mem_free(&outgoing_pdu);
904                 return False;
905         }
906
907         if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &outgoing_pdu, 0)) {
908                 DEBUG(0,("setup_fault_pdu: failed to marshall RPC_HDR_RESP.\n"));
909                 prs_mem_free(&outgoing_pdu);
910                 return False;
911         }
912
913         if(!smb_io_rpc_hdr_fault("fault", &fault_resp, &outgoing_pdu, 0)) {
914                 DEBUG(0,("setup_fault_pdu: failed to marshall RPC_HDR_FAULT.\n"));
915                 prs_mem_free(&outgoing_pdu);
916                 return False;
917         }
918
919         p->out_data.data_sent_length = 0;
920         p->out_data.current_pdu_len = prs_offset(&outgoing_pdu);
921         p->out_data.current_pdu_sent = 0;
922
923         prs_mem_free(&outgoing_pdu);
924         return True;
925 }
926
927 #if 0
928 /*******************************************************************
929  Marshall a cancel_ack pdu.
930  We should probably check the auth-verifier here.
931 *******************************************************************/
932
933 bool setup_cancel_ack_reply(pipes_struct *p, prs_struct *rpc_in_p)
934 {
935         prs_struct outgoing_pdu;
936         RPC_HDR ack_reply_hdr;
937
938         /* Free any memory in the current return data buffer. */
939         prs_mem_free(&p->out_data.rdata);
940
941         /*
942          * Marshall directly into the outgoing PDU space. We
943          * must do this as we need to set to the bind response
944          * header and are never sending more than one PDU here.
945          */
946
947         prs_init( &outgoing_pdu, 0, p->mem_ctx, MARSHALL);
948         prs_give_memory( &outgoing_pdu, (char *)p->out_data.current_pdu, sizeof(p->out_data.current_pdu), False);
949
950         /*
951          * Initialize a cancel_ack header.
952          */
953
954         init_rpc_hdr(&ack_reply_hdr, RPC_CANCEL_ACK, RPC_FLG_FIRST | RPC_FLG_LAST,
955                         p->hdr.call_id, RPC_HEADER_LEN, 0);
956
957         /*
958          * Marshall the header into the outgoing PDU.
959          */
960
961         if(!smb_io_rpc_hdr("", &ack_reply_hdr, &outgoing_pdu, 0)) {
962                 DEBUG(0,("setup_cancel_ack_reply: marshalling of RPC_HDR failed.\n"));
963                 prs_mem_free(&outgoing_pdu);
964                 return False;
965         }
966
967         p->out_data.data_sent_length = 0;
968         p->out_data.current_pdu_len = prs_offset(&outgoing_pdu);
969         p->out_data.current_pdu_sent = 0;
970
971         prs_mem_free(&outgoing_pdu);
972         return True;
973 }
974 #endif
975
976 /*******************************************************************
977  Ensure a bind request has the correct abstract & transfer interface.
978  Used to reject unknown binds from Win2k.
979 *******************************************************************/
980
981 bool check_bind_req(struct pipes_struct *p, RPC_IFACE* abstract,
982                     RPC_IFACE* transfer, uint32 context_id)
983 {
984         char *pipe_name = p->name;
985         int i=0;
986         fstring pname;
987         
988         fstrcpy(pname,"\\PIPE\\");
989         fstrcat(pname,pipe_name);
990
991         DEBUG(3,("check_bind_req for %s\n", pname));
992
993         /* we have to check all now since win2k introduced a new UUID on the lsaprpc pipe */
994                 
995         for ( i=0; pipe_names[i].client_pipe; i++ ) {
996                 DEBUG(10,("checking %s\n", pipe_names[i].client_pipe));
997                 if ( strequal(pipe_names[i].client_pipe, pname)
998                         && (abstract->version == pipe_names[i].abstr_syntax.version) 
999                         && (memcmp(&abstract->uuid, &pipe_names[i].abstr_syntax.uuid, sizeof(struct GUID)) == 0)
1000                         && (transfer->version == pipe_names[i].trans_syntax.version)
1001                         && (memcmp(&transfer->uuid, &pipe_names[i].trans_syntax.uuid, sizeof(struct GUID)) == 0) ) {
1002                         struct api_struct       *fns = NULL;
1003                         int                     n_fns = 0;
1004                         PIPE_RPC_FNS            *context_fns;
1005                         
1006                         if ( !(context_fns = SMB_MALLOC_P(PIPE_RPC_FNS)) ) {
1007                                 DEBUG(0,("check_bind_req: malloc() failed!\n"));
1008                                 return False;
1009                         }
1010                         
1011                         /* save the RPC function table associated with this bind */
1012                         
1013                         get_pipe_fns(i, &fns, &n_fns);
1014                         
1015                         context_fns->cmds = fns;
1016                         context_fns->n_cmds = n_fns;
1017                         context_fns->context_id = context_id;
1018                         
1019                         /* add to the list of open contexts */
1020                         
1021                         DLIST_ADD( p->contexts, context_fns );
1022                         
1023                         break;
1024                 }
1025         }
1026
1027         if(pipe_names[i].client_pipe == NULL) {
1028                 return False;
1029         }
1030
1031         return True;
1032 }
1033
1034 /*******************************************************************
1035  Register commands to an RPC pipe
1036 *******************************************************************/
1037
1038 NTSTATUS rpc_pipe_register_commands(int version, const char *clnt, const char *srv, const struct api_struct *cmds, int size)
1039 {
1040         struct rpc_table *rpc_entry;
1041
1042         if (!clnt || !srv || !cmds) {
1043                 return NT_STATUS_INVALID_PARAMETER;
1044         }
1045
1046         if (version != SMB_RPC_INTERFACE_VERSION) {
1047                 DEBUG(0,("Can't register rpc commands!\n"
1048                          "You tried to register a rpc module with SMB_RPC_INTERFACE_VERSION %d"
1049                          ", while this version of samba uses version %d!\n", 
1050                          version,SMB_RPC_INTERFACE_VERSION));
1051                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1052         }
1053
1054         /* TODO: 
1055          *
1056          * we still need to make sure that don't register the same commands twice!!!
1057          * 
1058          * --metze
1059          */
1060
1061         /* We use a temporary variable because this call can fail and 
1062            rpc_lookup will still be valid afterwards.  It could then succeed if
1063            called again later */
1064         rpc_lookup_size++;
1065         rpc_entry = SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(rpc_lookup, struct rpc_table, rpc_lookup_size);
1066         if (NULL == rpc_entry) {
1067                 rpc_lookup_size--;
1068                 DEBUG(0, ("rpc_pipe_register_commands: memory allocation failed\n"));
1069                 return NT_STATUS_NO_MEMORY;
1070         } else {
1071                 rpc_lookup = rpc_entry;
1072         }
1073         
1074         rpc_entry = rpc_lookup + (rpc_lookup_size - 1);
1075         ZERO_STRUCTP(rpc_entry);
1076         rpc_entry->pipe.clnt = SMB_STRDUP(clnt);
1077         rpc_entry->pipe.srv = SMB_STRDUP(srv);
1078         rpc_entry->cmds = SMB_REALLOC_ARRAY(rpc_entry->cmds, struct api_struct, rpc_entry->n_cmds + size);
1079         if (!rpc_entry->cmds) {
1080                 return NT_STATUS_NO_MEMORY;
1081         }
1082         memcpy(rpc_entry->cmds + rpc_entry->n_cmds, cmds, size * sizeof(struct api_struct));
1083         rpc_entry->n_cmds += size;
1084         
1085         return NT_STATUS_OK;
1086 }
1087
1088 /*******************************************************************
1089  Handle a SPNEGO krb5 bind auth.
1090 *******************************************************************/
1091
1092 static bool pipe_spnego_auth_bind_kerberos(pipes_struct *p, prs_struct *rpc_in_p, RPC_HDR_AUTH *pauth_info,
1093                 DATA_BLOB *psecblob, prs_struct *pout_auth)
1094 {
1095         return False;
1096 }
1097
1098 /*******************************************************************
1099  Handle the first part of a SPNEGO bind auth.
1100 *******************************************************************/
1101
1102 static bool pipe_spnego_auth_bind_negotiate(pipes_struct *p, prs_struct *rpc_in_p,
1103                                         RPC_HDR_AUTH *pauth_info, prs_struct *pout_auth)
1104 {
1105         DATA_BLOB blob;
1106         DATA_BLOB secblob;
1107         DATA_BLOB response;
1108         DATA_BLOB chal;
1109         char *OIDs[ASN1_MAX_OIDS];
1110         int i;
1111         NTSTATUS status;
1112         bool got_kerberos_mechanism = False;
1113         AUTH_NTLMSSP_STATE *a = NULL;
1114         RPC_HDR_AUTH auth_info;
1115
1116         ZERO_STRUCT(secblob);
1117         ZERO_STRUCT(chal);
1118         ZERO_STRUCT(response);
1119
1120         /* Grab the SPNEGO blob. */
1121         blob = data_blob(NULL,p->hdr.auth_len);
1122
1123         if (!prs_copy_data_out((char *)blob.data, rpc_in_p, p->hdr.auth_len)) {
1124                 DEBUG(0,("pipe_spnego_auth_bind_negotiate: Failed to pull %u bytes - the SPNEGO auth header.\n",
1125                         (unsigned int)p->hdr.auth_len ));
1126                 goto err;
1127         }
1128
1129         if (blob.data[0] != ASN1_APPLICATION(0)) {
1130                 goto err;
1131         }
1132
1133         /* parse out the OIDs and the first sec blob */
1134         if (!parse_negTokenTarg(blob, OIDs, &secblob)) {
1135                 DEBUG(0,("pipe_spnego_auth_bind_negotiate: Failed to parse the security blob.\n"));
1136                 goto err;
1137         }
1138
1139         if (strcmp(OID_KERBEROS5, OIDs[0]) == 0 || strcmp(OID_KERBEROS5_OLD, OIDs[0]) == 0) {
1140                 got_kerberos_mechanism = True;
1141         }
1142
1143         for (i=0;OIDs[i];i++) {
1144                 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got OID %s\n", OIDs[i]));
1145                 SAFE_FREE(OIDs[i]);
1146         }
1147         DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got secblob of size %lu\n", (unsigned long)secblob.length));
1148
1149         if ( got_kerberos_mechanism && ((lp_security()==SEC_ADS) || lp_use_kerberos_keytab()) ) {
1150                 bool ret = pipe_spnego_auth_bind_kerberos(p, rpc_in_p, pauth_info, &secblob, pout_auth);
1151                 data_blob_free(&secblob);
1152                 data_blob_free(&blob);
1153                 return ret;
1154         }
1155
1156         if (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP && p->auth.a_u.auth_ntlmssp_state) {
1157                 /* Free any previous auth type. */
1158                 free_pipe_ntlmssp_auth_data(&p->auth);
1159         }
1160
1161         /* Initialize the NTLM engine. */
1162         status = auth_ntlmssp_start(&a);
1163         if (!NT_STATUS_IS_OK(status)) {
1164                 goto err;
1165         }
1166
1167         /*
1168          * Pass the first security blob of data to it.
1169          * This can return an error or NT_STATUS_MORE_PROCESSING_REQUIRED
1170          * which means we need another packet to complete the bind.
1171          */
1172
1173         status = auth_ntlmssp_update(a, secblob, &chal);
1174
1175         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1176                 DEBUG(3,("pipe_spnego_auth_bind_negotiate: auth_ntlmssp_update failed.\n"));
1177                 goto err;
1178         }
1179
1180         /* Generate the response blob we need for step 2 of the bind. */
1181         response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
1182
1183         /* Copy the blob into the pout_auth parse struct */
1184         init_rpc_hdr_auth(&auth_info, RPC_SPNEGO_AUTH_TYPE, pauth_info->auth_level, RPC_HDR_AUTH_LEN, 1);
1185         if(!smb_io_rpc_hdr_auth("", &auth_info, pout_auth, 0)) {
1186                 DEBUG(0,("pipe_spnego_auth_bind_negotiate: marshalling of RPC_HDR_AUTH failed.\n"));
1187                 goto err;
1188         }
1189
1190         if (!prs_copy_data_in(pout_auth, (char *)response.data, response.length)) {
1191                 DEBUG(0,("pipe_spnego_auth_bind_negotiate: marshalling of data blob failed.\n"));
1192                 goto err;
1193         }
1194
1195         p->auth.a_u.auth_ntlmssp_state = a;
1196         p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
1197         p->auth.auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1198
1199         data_blob_free(&blob);
1200         data_blob_free(&secblob);
1201         data_blob_free(&chal);
1202         data_blob_free(&response);
1203
1204         /* We can't set pipe_bound True yet - we need an RPC_ALTER_CONTEXT response packet... */
1205         return True;
1206
1207  err:
1208
1209         data_blob_free(&blob);
1210         data_blob_free(&secblob);
1211         data_blob_free(&chal);
1212         data_blob_free(&response);
1213
1214         p->auth.a_u.auth_ntlmssp_state = NULL;
1215
1216         return False;
1217 }
1218
1219 /*******************************************************************
1220  Handle the second part of a SPNEGO bind auth.
1221 *******************************************************************/
1222
1223 static bool pipe_spnego_auth_bind_continue(pipes_struct *p, prs_struct *rpc_in_p,
1224                                         RPC_HDR_AUTH *pauth_info, prs_struct *pout_auth)
1225 {
1226         RPC_HDR_AUTH auth_info;
1227         DATA_BLOB spnego_blob;
1228         DATA_BLOB auth_blob;
1229         DATA_BLOB auth_reply;
1230         DATA_BLOB response;
1231         AUTH_NTLMSSP_STATE *a = p->auth.a_u.auth_ntlmssp_state;
1232
1233         ZERO_STRUCT(spnego_blob);
1234         ZERO_STRUCT(auth_blob);
1235         ZERO_STRUCT(auth_reply);
1236         ZERO_STRUCT(response);
1237
1238         if (p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP || !a) {
1239                 DEBUG(0,("pipe_spnego_auth_bind_continue: not in NTLMSSP auth state.\n"));
1240                 goto err;
1241         }
1242
1243         /* Grab the SPNEGO blob. */
1244         spnego_blob = data_blob(NULL,p->hdr.auth_len);
1245
1246         if (!prs_copy_data_out((char *)spnego_blob.data, rpc_in_p, p->hdr.auth_len)) {
1247                 DEBUG(0,("pipe_spnego_auth_bind_continue: Failed to pull %u bytes - the SPNEGO auth header.\n",
1248                         (unsigned int)p->hdr.auth_len ));
1249                 goto err;
1250         }
1251
1252         if (spnego_blob.data[0] != ASN1_CONTEXT(1)) {
1253                 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob type.\n"));
1254                 goto err;
1255         }
1256
1257         if (!spnego_parse_auth(spnego_blob, &auth_blob)) {
1258                 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob.\n"));
1259                 goto err;
1260         }
1261
1262         /*
1263          * The following call actually checks the challenge/response data.
1264          * for correctness against the given DOMAIN\user name.
1265          */
1266         
1267         if (!pipe_ntlmssp_verify_final(p, &auth_blob)) {
1268                 goto err;
1269         }
1270
1271         data_blob_free(&spnego_blob);
1272         data_blob_free(&auth_blob);
1273
1274         /* Generate the spnego "accept completed" blob - no incoming data. */
1275         response = spnego_gen_auth_response(&auth_reply, NT_STATUS_OK, OID_NTLMSSP);
1276
1277         /* Copy the blob into the pout_auth parse struct */
1278         init_rpc_hdr_auth(&auth_info, RPC_SPNEGO_AUTH_TYPE, pauth_info->auth_level, RPC_HDR_AUTH_LEN, 1);
1279         if(!smb_io_rpc_hdr_auth("", &auth_info, pout_auth, 0)) {
1280                 DEBUG(0,("pipe_spnego_auth_bind_continue: marshalling of RPC_HDR_AUTH failed.\n"));
1281                 goto err;
1282         }
1283
1284         if (!prs_copy_data_in(pout_auth, (char *)response.data, response.length)) {
1285                 DEBUG(0,("pipe_spnego_auth_bind_continue: marshalling of data blob failed.\n"));
1286                 goto err;
1287         }
1288
1289         data_blob_free(&auth_reply);
1290         data_blob_free(&response);
1291
1292         p->pipe_bound = True;
1293
1294         return True;
1295
1296  err:
1297
1298         data_blob_free(&spnego_blob);
1299         data_blob_free(&auth_blob);
1300         data_blob_free(&auth_reply);
1301         data_blob_free(&response);
1302
1303         free_pipe_ntlmssp_auth_data(&p->auth);
1304         p->auth.a_u.auth_ntlmssp_state = NULL;
1305
1306         return False;
1307 }
1308
1309 /*******************************************************************
1310  Handle an schannel bind auth.
1311 *******************************************************************/
1312
1313 static bool pipe_schannel_auth_bind(pipes_struct *p, prs_struct *rpc_in_p,
1314                                         RPC_HDR_AUTH *pauth_info, prs_struct *pout_auth)
1315 {
1316         RPC_HDR_AUTH auth_info;
1317         RPC_AUTH_SCHANNEL_NEG neg;
1318         RPC_AUTH_VERIFIER auth_verifier;
1319         bool ret;
1320         struct dcinfo *pdcinfo;
1321         uint32 flags;
1322
1323         if (!smb_io_rpc_auth_schannel_neg("", &neg, rpc_in_p, 0)) {
1324                 DEBUG(0,("pipe_schannel_auth_bind: Could not unmarshal SCHANNEL auth neg\n"));
1325                 return False;
1326         }
1327
1328         /*
1329          * The neg.myname key here must match the remote computer name
1330          * given in the DOM_CLNT_SRV.uni_comp_name used on all netlogon pipe
1331          * operations that use credentials.
1332          */
1333
1334         become_root();
1335         ret = secrets_restore_schannel_session_info(p->mem_ctx, neg.myname, &pdcinfo);
1336         unbecome_root();
1337
1338         if (!ret) {
1339                 DEBUG(0, ("pipe_schannel_auth_bind: Attempt to bind using schannel without successful serverauth2\n"));
1340                 return False;
1341         }
1342
1343         p->auth.a_u.schannel_auth = TALLOC_P(p->pipe_state_mem_ctx, struct schannel_auth_struct);
1344         if (!p->auth.a_u.schannel_auth) {
1345                 TALLOC_FREE(pdcinfo);
1346                 return False;
1347         }
1348
1349         memset(p->auth.a_u.schannel_auth->sess_key, 0, sizeof(p->auth.a_u.schannel_auth->sess_key));
1350         memcpy(p->auth.a_u.schannel_auth->sess_key, pdcinfo->sess_key,
1351                         sizeof(pdcinfo->sess_key));
1352
1353         TALLOC_FREE(pdcinfo);
1354
1355         p->auth.a_u.schannel_auth->seq_num = 0;
1356
1357         /*
1358          * JRA. Should we also copy the schannel session key into the pipe session key p->session_key
1359          * here ? We do that for NTLMSSP, but the session key is already set up from the vuser
1360          * struct of the person who opened the pipe. I need to test this further. JRA.
1361          *
1362          * VL. As we are mapping this to guest set the generic key
1363          * "SystemLibraryDTC" key here. It's a bit difficult to test against
1364          * W2k3, as it does not allow schannel binds against SAMR and LSA
1365          * anymore.
1366          */
1367
1368         data_blob_free(&p->session_key);
1369         p->session_key = generic_session_key();
1370         if (p->session_key.data == NULL) {
1371                 DEBUG(0, ("pipe_schannel_auth_bind: Could not alloc session"
1372                           " key\n"));
1373                 return False;
1374         }
1375
1376         init_rpc_hdr_auth(&auth_info, RPC_SCHANNEL_AUTH_TYPE, pauth_info->auth_level, RPC_HDR_AUTH_LEN, 1);
1377         if(!smb_io_rpc_hdr_auth("", &auth_info, pout_auth, 0)) {
1378                 DEBUG(0,("pipe_schannel_auth_bind: marshalling of RPC_HDR_AUTH failed.\n"));
1379                 return False;
1380         }
1381
1382         /*** SCHANNEL verifier ***/
1383
1384         init_rpc_auth_verifier(&auth_verifier, "\001", 0x0);
1385         if(!smb_io_rpc_schannel_verifier("", &auth_verifier, pout_auth, 0)) {
1386                 DEBUG(0,("pipe_schannel_auth_bind: marshalling of RPC_AUTH_VERIFIER failed.\n"));
1387                 return False;
1388         }
1389
1390         prs_align(pout_auth);
1391
1392         flags = 5;
1393         if(!prs_uint32("flags ", pout_auth, 0, &flags)) {
1394                 return False;
1395         }
1396
1397         DEBUG(10,("pipe_schannel_auth_bind: schannel auth: domain [%s] myname [%s]\n",
1398                 neg.domain, neg.myname));
1399
1400         /* We're finished with this bind - no more packets. */
1401         p->auth.auth_data_free_func = NULL;
1402         p->auth.auth_type = PIPE_AUTH_TYPE_SCHANNEL;
1403
1404         p->pipe_bound = True;
1405
1406         return True;
1407 }
1408
1409 /*******************************************************************
1410  Handle an NTLMSSP bind auth.
1411 *******************************************************************/
1412
1413 static bool pipe_ntlmssp_auth_bind(pipes_struct *p, prs_struct *rpc_in_p,
1414                                         RPC_HDR_AUTH *pauth_info, prs_struct *pout_auth)
1415 {
1416         RPC_HDR_AUTH auth_info;
1417         DATA_BLOB blob;
1418         DATA_BLOB response;
1419         NTSTATUS status;
1420         AUTH_NTLMSSP_STATE *a = NULL;
1421
1422         ZERO_STRUCT(blob);
1423         ZERO_STRUCT(response);
1424
1425         /* Grab the NTLMSSP blob. */
1426         blob = data_blob(NULL,p->hdr.auth_len);
1427
1428         if (!prs_copy_data_out((char *)blob.data, rpc_in_p, p->hdr.auth_len)) {
1429                 DEBUG(0,("pipe_ntlmssp_auth_bind: Failed to pull %u bytes - the NTLM auth header.\n",
1430                         (unsigned int)p->hdr.auth_len ));
1431                 goto err;
1432         }
1433
1434         if (strncmp((char *)blob.data, "NTLMSSP", 7) != 0) {
1435                 DEBUG(0,("pipe_ntlmssp_auth_bind: Failed to read NTLMSSP in blob\n"));
1436                 goto err;
1437         }
1438
1439         /* We have an NTLMSSP blob. */
1440         status = auth_ntlmssp_start(&a);
1441         if (!NT_STATUS_IS_OK(status)) {
1442                 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_start failed: %s\n",
1443                         nt_errstr(status) ));
1444                 goto err;
1445         }
1446
1447         status = auth_ntlmssp_update(a, blob, &response);
1448         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1449                 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_update failed: %s\n",
1450                         nt_errstr(status) ));
1451                 goto err;
1452         }
1453
1454         data_blob_free(&blob);
1455
1456         /* Copy the blob into the pout_auth parse struct */
1457         init_rpc_hdr_auth(&auth_info, RPC_NTLMSSP_AUTH_TYPE, pauth_info->auth_level, RPC_HDR_AUTH_LEN, 1);
1458         if(!smb_io_rpc_hdr_auth("", &auth_info, pout_auth, 0)) {
1459                 DEBUG(0,("pipe_ntlmssp_auth_bind: marshalling of RPC_HDR_AUTH failed.\n"));
1460                 goto err;
1461         }
1462
1463         if (!prs_copy_data_in(pout_auth, (char *)response.data, response.length)) {
1464                 DEBUG(0,("pipe_ntlmssp_auth_bind: marshalling of data blob failed.\n"));
1465                 goto err;
1466         }
1467
1468         p->auth.a_u.auth_ntlmssp_state = a;
1469         p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
1470         p->auth.auth_type = PIPE_AUTH_TYPE_NTLMSSP;
1471
1472         data_blob_free(&blob);
1473         data_blob_free(&response);
1474
1475         DEBUG(10,("pipe_ntlmssp_auth_bind: NTLMSSP auth started\n"));
1476
1477         /* We can't set pipe_bound True yet - we need an RPC_AUTH3 response packet... */
1478         return True;
1479
1480   err:
1481
1482         data_blob_free(&blob);
1483         data_blob_free(&response);
1484
1485         free_pipe_ntlmssp_auth_data(&p->auth);
1486         p->auth.a_u.auth_ntlmssp_state = NULL;
1487         return False;
1488 }
1489
1490 /*******************************************************************
1491  Respond to a pipe bind request.
1492 *******************************************************************/
1493
1494 bool api_pipe_bind_req(pipes_struct *p, prs_struct *rpc_in_p)
1495 {
1496         RPC_HDR_BA hdr_ba;
1497         RPC_HDR_RB hdr_rb;
1498         RPC_HDR_AUTH auth_info;
1499         uint16 assoc_gid;
1500         fstring ack_pipe_name;
1501         prs_struct out_hdr_ba;
1502         prs_struct out_auth;
1503         prs_struct outgoing_rpc;
1504         int i = 0;
1505         int auth_len = 0;
1506         unsigned int auth_type = RPC_ANONYMOUS_AUTH_TYPE;
1507
1508         /* No rebinds on a bound pipe - use alter context. */
1509         if (p->pipe_bound) {
1510                 DEBUG(2,("api_pipe_bind_req: rejecting bind request on bound pipe %s.\n", p->pipe_srv_name));
1511                 return setup_bind_nak(p);
1512         }
1513
1514         prs_init( &outgoing_rpc, 0, p->mem_ctx, MARSHALL);
1515
1516         /* 
1517          * Marshall directly into the outgoing PDU space. We
1518          * must do this as we need to set to the bind response
1519          * header and are never sending more than one PDU here.
1520          */
1521
1522         prs_give_memory( &outgoing_rpc, (char *)p->out_data.current_pdu, sizeof(p->out_data.current_pdu), False);
1523
1524         /*
1525          * Setup the memory to marshall the ba header, and the
1526          * auth footers.
1527          */
1528
1529         if(!prs_init(&out_hdr_ba, 1024, p->mem_ctx, MARSHALL)) {
1530                 DEBUG(0,("api_pipe_bind_req: malloc out_hdr_ba failed.\n"));
1531                 prs_mem_free(&outgoing_rpc);
1532                 return False;
1533         }
1534
1535         if(!prs_init(&out_auth, 1024, p->mem_ctx, MARSHALL)) {
1536                 DEBUG(0,("api_pipe_bind_req: malloc out_auth failed.\n"));
1537                 prs_mem_free(&outgoing_rpc);
1538                 prs_mem_free(&out_hdr_ba);
1539                 return False;
1540         }
1541
1542         DEBUG(5,("api_pipe_bind_req: decode request. %d\n", __LINE__));
1543
1544         /*
1545          * Try and find the correct pipe name to ensure
1546          * that this is a pipe name we support.
1547          */
1548
1549
1550         for (i = 0; i < rpc_lookup_size; i++) {
1551                 if (strequal(rpc_lookup[i].pipe.clnt, p->name)) {
1552                         DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1553                                 rpc_lookup[i].pipe.clnt, rpc_lookup[i].pipe.srv));
1554                         fstrcpy(p->pipe_srv_name, rpc_lookup[i].pipe.srv);
1555                         break;
1556                 }
1557         }
1558
1559         if (i == rpc_lookup_size) {
1560                 if (NT_STATUS_IS_ERR(smb_probe_module("rpc", p->name))) {
1561                        DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
1562                                 p->name ));
1563                         prs_mem_free(&outgoing_rpc);
1564                         prs_mem_free(&out_hdr_ba);
1565                         prs_mem_free(&out_auth);
1566
1567                         return setup_bind_nak(p);
1568                 }
1569
1570                 for (i = 0; i < rpc_lookup_size; i++) {
1571                        if (strequal(rpc_lookup[i].pipe.clnt, p->name)) {
1572                                DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1573                                          rpc_lookup[i].pipe.clnt, rpc_lookup[i].pipe.srv));
1574                                fstrcpy(p->pipe_srv_name, rpc_lookup[i].pipe.srv);
1575                                break;
1576                        }
1577                 }
1578
1579                 if (i == rpc_lookup_size) {
1580                         DEBUG(0, ("module %s doesn't provide functions for pipe %s!\n", p->name, p->name));
1581                         goto err_exit;
1582                 }
1583         }
1584
1585         /* decode the bind request */
1586         if(!smb_io_rpc_hdr_rb("", &hdr_rb, rpc_in_p, 0))  {
1587                 DEBUG(0,("api_pipe_bind_req: unable to unmarshall RPC_HDR_RB struct.\n"));
1588                 goto err_exit;
1589         }
1590
1591         /* name has to be \PIPE\xxxxx */
1592         fstrcpy(ack_pipe_name, "\\PIPE\\");
1593         fstrcat(ack_pipe_name, p->pipe_srv_name);
1594
1595         DEBUG(5,("api_pipe_bind_req: make response. %d\n", __LINE__));
1596
1597         /*
1598          * Check if this is an authenticated bind request.
1599          */
1600
1601         if (p->hdr.auth_len) {
1602                 /* 
1603                  * Decode the authentication verifier.
1604                  */
1605
1606                 if(!smb_io_rpc_hdr_auth("", &auth_info, rpc_in_p, 0)) {
1607                         DEBUG(0,("api_pipe_bind_req: unable to unmarshall RPC_HDR_AUTH struct.\n"));
1608                         goto err_exit;
1609                 }
1610
1611                 auth_type = auth_info.auth_type;
1612
1613                 /* Work out if we have to sign or seal etc. */
1614                 switch (auth_info.auth_level) {
1615                         case RPC_AUTH_LEVEL_INTEGRITY:
1616                                 p->auth.auth_level = PIPE_AUTH_LEVEL_INTEGRITY;
1617                                 break;
1618                         case RPC_AUTH_LEVEL_PRIVACY:
1619                                 p->auth.auth_level = PIPE_AUTH_LEVEL_PRIVACY;
1620                                 break;
1621                         default:
1622                                 DEBUG(0,("api_pipe_bind_req: unexpected auth level (%u).\n",
1623                                         (unsigned int)auth_info.auth_level ));
1624                                 goto err_exit;
1625                 }
1626         } else {
1627                 ZERO_STRUCT(auth_info);
1628         }
1629
1630         assoc_gid = hdr_rb.bba.assoc_gid ? hdr_rb.bba.assoc_gid : 0x53f0;
1631
1632         switch(auth_type) {
1633                 case RPC_NTLMSSP_AUTH_TYPE:
1634                         if (!pipe_ntlmssp_auth_bind(p, rpc_in_p, &auth_info, &out_auth)) {
1635                                 goto err_exit;
1636                         }
1637                         assoc_gid = 0x7a77;
1638                         break;
1639
1640                 case RPC_SCHANNEL_AUTH_TYPE:
1641                         if (!pipe_schannel_auth_bind(p, rpc_in_p, &auth_info, &out_auth)) {
1642                                 goto err_exit;
1643                         }
1644                         break;
1645
1646                 case RPC_SPNEGO_AUTH_TYPE:
1647                         if (!pipe_spnego_auth_bind_negotiate(p, rpc_in_p, &auth_info, &out_auth)) {
1648                                 goto err_exit;
1649                         }
1650                         break;
1651
1652                 case RPC_ANONYMOUS_AUTH_TYPE:
1653                         /* Unauthenticated bind request. */
1654                         /* Get the authenticated pipe user from current_user */
1655                         if (!copy_current_user(&p->pipe_user, &current_user)) {
1656                                 DEBUG(10, ("Could not copy current user\n"));
1657                                 goto err_exit;
1658                         }
1659                         /* We're finished - no more packets. */
1660                         p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
1661                         /* We must set the pipe auth_level here also. */
1662                         p->auth.auth_level = PIPE_AUTH_LEVEL_NONE;
1663                         p->pipe_bound = True;
1664                         /* The session key was initialized from the SMB
1665                          * session in make_internal_rpc_pipe_p */
1666                         break;
1667
1668                 default:
1669                         DEBUG(0,("api_pipe_bind_req: unknown auth type %x requested.\n", auth_type ));
1670                         goto err_exit;
1671         }
1672
1673         /*
1674          * Create the bind response struct.
1675          */
1676
1677         /* If the requested abstract synt uuid doesn't match our client pipe,
1678                 reject the bind_ack & set the transfer interface synt to all 0's,
1679                 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1680                 unknown to NT4)
1681                 Needed when adding entries to a DACL from NT5 - SK */
1682
1683         if(check_bind_req(p, &hdr_rb.rpc_context[0].abstract, &hdr_rb.rpc_context[0].transfer[0],
1684                                 hdr_rb.rpc_context[0].context_id )) {
1685                 init_rpc_hdr_ba(&hdr_ba,
1686                         RPC_MAX_PDU_FRAG_LEN,
1687                         RPC_MAX_PDU_FRAG_LEN,
1688                         assoc_gid,
1689                         ack_pipe_name,
1690                         0x1, 0x0, 0x0,
1691                         &hdr_rb.rpc_context[0].transfer[0]);
1692         } else {
1693                 RPC_IFACE null_interface;
1694                 ZERO_STRUCT(null_interface);
1695                 /* Rejection reason: abstract syntax not supported */
1696                 init_rpc_hdr_ba(&hdr_ba, RPC_MAX_PDU_FRAG_LEN,
1697                                         RPC_MAX_PDU_FRAG_LEN, assoc_gid,
1698                                         ack_pipe_name, 0x1, 0x2, 0x1,
1699                                         &null_interface);
1700                 p->pipe_bound = False;
1701         }
1702
1703         /*
1704          * and marshall it.
1705          */
1706
1707         if(!smb_io_rpc_hdr_ba("", &hdr_ba, &out_hdr_ba, 0)) {
1708                 DEBUG(0,("api_pipe_bind_req: marshalling of RPC_HDR_BA failed.\n"));
1709                 goto err_exit;
1710         }
1711
1712         /*
1713          * Create the header, now we know the length.
1714          */
1715
1716         if (prs_offset(&out_auth)) {
1717                 auth_len = prs_offset(&out_auth) - RPC_HDR_AUTH_LEN;
1718         }
1719
1720         init_rpc_hdr(&p->hdr, RPC_BINDACK, RPC_FLG_FIRST | RPC_FLG_LAST,
1721                         p->hdr.call_id,
1722                         RPC_HEADER_LEN + prs_offset(&out_hdr_ba) + prs_offset(&out_auth),
1723                         auth_len);
1724
1725         /*
1726          * Marshall the header into the outgoing PDU.
1727          */
1728
1729         if(!smb_io_rpc_hdr("", &p->hdr, &outgoing_rpc, 0)) {
1730                 DEBUG(0,("api_pipe_bind_req: marshalling of RPC_HDR failed.\n"));
1731                 goto err_exit;
1732         }
1733
1734         /*
1735          * Now add the RPC_HDR_BA and any auth needed.
1736          */
1737
1738         if(!prs_append_prs_data( &outgoing_rpc, &out_hdr_ba)) {
1739                 DEBUG(0,("api_pipe_bind_req: append of RPC_HDR_BA failed.\n"));
1740                 goto err_exit;
1741         }
1742
1743         if (auth_len && !prs_append_prs_data( &outgoing_rpc, &out_auth)) {
1744                 DEBUG(0,("api_pipe_bind_req: append of auth info failed.\n"));
1745                 goto err_exit;
1746         }
1747
1748         /*
1749          * Setup the lengths for the initial reply.
1750          */
1751
1752         p->out_data.data_sent_length = 0;
1753         p->out_data.current_pdu_len = prs_offset(&outgoing_rpc);
1754         p->out_data.current_pdu_sent = 0;
1755
1756         prs_mem_free(&out_hdr_ba);
1757         prs_mem_free(&out_auth);
1758
1759         return True;
1760
1761   err_exit:
1762
1763         prs_mem_free(&outgoing_rpc);
1764         prs_mem_free(&out_hdr_ba);
1765         prs_mem_free(&out_auth);
1766         return setup_bind_nak(p);
1767 }
1768
1769 /****************************************************************************
1770  Deal with an alter context call. Can be third part of 3 leg auth request for
1771  SPNEGO calls.
1772 ****************************************************************************/
1773
1774 bool api_pipe_alter_context(pipes_struct *p, prs_struct *rpc_in_p)
1775 {
1776         RPC_HDR_BA hdr_ba;
1777         RPC_HDR_RB hdr_rb;
1778         RPC_HDR_AUTH auth_info;
1779         uint16 assoc_gid;
1780         fstring ack_pipe_name;
1781         prs_struct out_hdr_ba;
1782         prs_struct out_auth;
1783         prs_struct outgoing_rpc;
1784         int auth_len = 0;
1785
1786         prs_init( &outgoing_rpc, 0, p->mem_ctx, MARSHALL);
1787
1788         /* 
1789          * Marshall directly into the outgoing PDU space. We
1790          * must do this as we need to set to the bind response
1791          * header and are never sending more than one PDU here.
1792          */
1793
1794         prs_give_memory( &outgoing_rpc, (char *)p->out_data.current_pdu, sizeof(p->out_data.current_pdu), False);
1795
1796         /*
1797          * Setup the memory to marshall the ba header, and the
1798          * auth footers.
1799          */
1800
1801         if(!prs_init(&out_hdr_ba, 1024, p->mem_ctx, MARSHALL)) {
1802                 DEBUG(0,("api_pipe_alter_context: malloc out_hdr_ba failed.\n"));
1803                 prs_mem_free(&outgoing_rpc);
1804                 return False;
1805         }
1806
1807         if(!prs_init(&out_auth, 1024, p->mem_ctx, MARSHALL)) {
1808                 DEBUG(0,("api_pipe_alter_context: malloc out_auth failed.\n"));
1809                 prs_mem_free(&outgoing_rpc);
1810                 prs_mem_free(&out_hdr_ba);
1811                 return False;
1812         }
1813
1814         DEBUG(5,("api_pipe_alter_context: decode request. %d\n", __LINE__));
1815
1816         /* decode the alter context request */
1817         if(!smb_io_rpc_hdr_rb("", &hdr_rb, rpc_in_p, 0))  {
1818                 DEBUG(0,("api_pipe_alter_context: unable to unmarshall RPC_HDR_RB struct.\n"));
1819                 goto err_exit;
1820         }
1821
1822         /* secondary address CAN be NULL
1823          * as the specs say it's ignored.
1824          * It MUST be NULL to have the spoolss working.
1825          */
1826         fstrcpy(ack_pipe_name,"");
1827
1828         DEBUG(5,("api_pipe_alter_context: make response. %d\n", __LINE__));
1829
1830         /*
1831          * Check if this is an authenticated alter context request.
1832          */
1833
1834         if (p->hdr.auth_len != 0) {
1835                 /* 
1836                  * Decode the authentication verifier.
1837                  */
1838
1839                 if(!smb_io_rpc_hdr_auth("", &auth_info, rpc_in_p, 0)) {
1840                         DEBUG(0,("api_pipe_alter_context: unable to unmarshall RPC_HDR_AUTH struct.\n"));
1841                         goto err_exit;
1842                 }
1843
1844                 /*
1845                  * Currently only the SPNEGO auth type uses the alter ctx
1846                  * response in place of the NTLMSSP auth3 type.
1847                  */
1848
1849                 if (auth_info.auth_type == RPC_SPNEGO_AUTH_TYPE) {
1850                         /* We can only finish if the pipe is unbound. */
1851                         if (!p->pipe_bound) {
1852                                 if (!pipe_spnego_auth_bind_continue(p, rpc_in_p, &auth_info, &out_auth)) {
1853                                         goto err_exit;
1854                                 }
1855                         } else {
1856                                 goto err_exit;
1857                         }
1858                 }
1859         } else {
1860                 ZERO_STRUCT(auth_info);
1861         }
1862
1863         assoc_gid = hdr_rb.bba.assoc_gid ? hdr_rb.bba.assoc_gid : 0x53f0;
1864
1865         /*
1866          * Create the bind response struct.
1867          */
1868
1869         /* If the requested abstract synt uuid doesn't match our client pipe,
1870                 reject the bind_ack & set the transfer interface synt to all 0's,
1871                 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1872                 unknown to NT4)
1873                 Needed when adding entries to a DACL from NT5 - SK */
1874
1875         if(check_bind_req(p, &hdr_rb.rpc_context[0].abstract, &hdr_rb.rpc_context[0].transfer[0],
1876                                 hdr_rb.rpc_context[0].context_id )) {
1877                 init_rpc_hdr_ba(&hdr_ba,
1878                         RPC_MAX_PDU_FRAG_LEN,
1879                         RPC_MAX_PDU_FRAG_LEN,
1880                         assoc_gid,
1881                         ack_pipe_name,
1882                         0x1, 0x0, 0x0,
1883                         &hdr_rb.rpc_context[0].transfer[0]);
1884         } else {
1885                 RPC_IFACE null_interface;
1886                 ZERO_STRUCT(null_interface);
1887                 /* Rejection reason: abstract syntax not supported */
1888                 init_rpc_hdr_ba(&hdr_ba, RPC_MAX_PDU_FRAG_LEN,
1889                                         RPC_MAX_PDU_FRAG_LEN, assoc_gid,
1890                                         ack_pipe_name, 0x1, 0x2, 0x1,
1891                                         &null_interface);
1892                 p->pipe_bound = False;
1893         }
1894
1895         /*
1896          * and marshall it.
1897          */
1898
1899         if(!smb_io_rpc_hdr_ba("", &hdr_ba, &out_hdr_ba, 0)) {
1900                 DEBUG(0,("api_pipe_alter_context: marshalling of RPC_HDR_BA failed.\n"));
1901                 goto err_exit;
1902         }
1903
1904         /*
1905          * Create the header, now we know the length.
1906          */
1907
1908         if (prs_offset(&out_auth)) {
1909                 auth_len = prs_offset(&out_auth) - RPC_HDR_AUTH_LEN;
1910         }
1911
1912         init_rpc_hdr(&p->hdr, RPC_ALTCONTRESP, RPC_FLG_FIRST | RPC_FLG_LAST,
1913                         p->hdr.call_id,
1914                         RPC_HEADER_LEN + prs_offset(&out_hdr_ba) + prs_offset(&out_auth),
1915                         auth_len);
1916
1917         /*
1918          * Marshall the header into the outgoing PDU.
1919          */
1920
1921         if(!smb_io_rpc_hdr("", &p->hdr, &outgoing_rpc, 0)) {
1922                 DEBUG(0,("api_pipe_alter_context: marshalling of RPC_HDR failed.\n"));
1923                 goto err_exit;
1924         }
1925
1926         /*
1927          * Now add the RPC_HDR_BA and any auth needed.
1928          */
1929
1930         if(!prs_append_prs_data( &outgoing_rpc, &out_hdr_ba)) {
1931                 DEBUG(0,("api_pipe_alter_context: append of RPC_HDR_BA failed.\n"));
1932                 goto err_exit;
1933         }
1934
1935         if (auth_len && !prs_append_prs_data( &outgoing_rpc, &out_auth)) {
1936                 DEBUG(0,("api_pipe_alter_context: append of auth info failed.\n"));
1937                 goto err_exit;
1938         }
1939
1940         /*
1941          * Setup the lengths for the initial reply.
1942          */
1943
1944         p->out_data.data_sent_length = 0;
1945         p->out_data.current_pdu_len = prs_offset(&outgoing_rpc);
1946         p->out_data.current_pdu_sent = 0;
1947
1948         prs_mem_free(&out_hdr_ba);
1949         prs_mem_free(&out_auth);
1950
1951         return True;
1952
1953   err_exit:
1954
1955         prs_mem_free(&outgoing_rpc);
1956         prs_mem_free(&out_hdr_ba);
1957         prs_mem_free(&out_auth);
1958         return setup_bind_nak(p);
1959 }
1960
1961 /****************************************************************************
1962  Deal with NTLMSSP sign & seal processing on an RPC request.
1963 ****************************************************************************/
1964
1965 bool api_pipe_ntlmssp_auth_process(pipes_struct *p, prs_struct *rpc_in,
1966                                         uint32 *p_ss_padding_len, NTSTATUS *pstatus)
1967 {
1968         RPC_HDR_AUTH auth_info;
1969         uint32 auth_len = p->hdr.auth_len;
1970         uint32 save_offset = prs_offset(rpc_in);
1971         AUTH_NTLMSSP_STATE *a = p->auth.a_u.auth_ntlmssp_state;
1972         unsigned char *data = NULL;
1973         size_t data_len;
1974         unsigned char *full_packet_data = NULL;
1975         size_t full_packet_data_len;
1976         DATA_BLOB auth_blob;
1977         
1978         *pstatus = NT_STATUS_OK;
1979
1980         if (p->auth.auth_level == PIPE_AUTH_LEVEL_NONE || p->auth.auth_level == PIPE_AUTH_LEVEL_CONNECT) {
1981                 return True;
1982         }
1983
1984         if (!a) {
1985                 *pstatus = NT_STATUS_INVALID_PARAMETER;
1986                 return False;
1987         }
1988
1989         /* Ensure there's enough data for an authenticated request. */
1990         if ((auth_len > RPC_MAX_SIGN_SIZE) ||
1991                         (RPC_HEADER_LEN + RPC_HDR_REQ_LEN + RPC_HDR_AUTH_LEN + auth_len > p->hdr.frag_len)) {
1992                 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len %u is too large.\n",
1993                         (unsigned int)auth_len ));
1994                 *pstatus = NT_STATUS_INVALID_PARAMETER;
1995                 return False;
1996         }
1997
1998         /*
1999          * We need the full packet data + length (minus auth stuff) as well as the packet data + length
2000          * after the RPC header. 
2001          * We need to pass in the full packet (minus auth len) to the NTLMSSP sign and check seal
2002          * functions as NTLMv2 checks the rpc headers also.
2003          */
2004
2005         data = (unsigned char *)(prs_data_p(rpc_in) + RPC_HDR_REQ_LEN);
2006         data_len = (size_t)(p->hdr.frag_len - RPC_HEADER_LEN - RPC_HDR_REQ_LEN - RPC_HDR_AUTH_LEN - auth_len);
2007
2008         full_packet_data = p->in_data.current_in_pdu;
2009         full_packet_data_len = p->hdr.frag_len - auth_len;
2010
2011         /* Pull the auth header and the following data into a blob. */
2012         if(!prs_set_offset(rpc_in, RPC_HDR_REQ_LEN + data_len)) {
2013                 DEBUG(0,("api_pipe_ntlmssp_auth_process: cannot move offset to %u.\n",
2014                         (unsigned int)RPC_HDR_REQ_LEN + (unsigned int)data_len ));
2015                 *pstatus = NT_STATUS_INVALID_PARAMETER;
2016                 return False;
2017         }
2018
2019         if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info, rpc_in, 0)) {
2020                 DEBUG(0,("api_pipe_ntlmssp_auth_process: failed to unmarshall RPC_HDR_AUTH.\n"));
2021                 *pstatus = NT_STATUS_INVALID_PARAMETER;
2022                 return False;
2023         }
2024
2025         auth_blob.data = (unsigned char *)prs_data_p(rpc_in) + prs_offset(rpc_in);
2026         auth_blob.length = auth_len;
2027         
2028         switch (p->auth.auth_level) {
2029                 case PIPE_AUTH_LEVEL_PRIVACY:
2030                         /* Data is encrypted. */
2031                         *pstatus = ntlmssp_unseal_packet(a->ntlmssp_state,
2032                                                         data, data_len,
2033                                                         full_packet_data,
2034                                                         full_packet_data_len,
2035                                                         &auth_blob);
2036                         if (!NT_STATUS_IS_OK(*pstatus)) {
2037                                 return False;
2038                         }
2039                         break;
2040                 case PIPE_AUTH_LEVEL_INTEGRITY:
2041                         /* Data is signed. */
2042                         *pstatus = ntlmssp_check_packet(a->ntlmssp_state,
2043                                                         data, data_len,
2044                                                         full_packet_data,
2045                                                         full_packet_data_len,
2046                                                         &auth_blob);
2047                         if (!NT_STATUS_IS_OK(*pstatus)) {
2048                                 return False;
2049                         }
2050                         break;
2051                 default:
2052                         *pstatus = NT_STATUS_INVALID_PARAMETER;
2053                         return False;
2054         }
2055
2056         /*
2057          * Return the current pointer to the data offset.
2058          */
2059
2060         if(!prs_set_offset(rpc_in, save_offset)) {
2061                 DEBUG(0,("api_pipe_auth_process: failed to set offset back to %u\n",
2062                         (unsigned int)save_offset ));
2063                 *pstatus = NT_STATUS_INVALID_PARAMETER;
2064                 return False;
2065         }
2066
2067         /*
2068          * Remember the padding length. We must remove it from the real data
2069          * stream once the sign/seal is done.
2070          */
2071
2072         *p_ss_padding_len = auth_info.auth_pad_len;
2073
2074         return True;
2075 }
2076
2077 /****************************************************************************
2078  Deal with schannel processing on an RPC request.
2079 ****************************************************************************/
2080
2081 bool api_pipe_schannel_process(pipes_struct *p, prs_struct *rpc_in, uint32 *p_ss_padding_len)
2082 {
2083         uint32 data_len;
2084         uint32 auth_len;
2085         uint32 save_offset = prs_offset(rpc_in);
2086         RPC_HDR_AUTH auth_info;
2087         RPC_AUTH_SCHANNEL_CHK schannel_chk;
2088
2089         auth_len = p->hdr.auth_len;
2090
2091         if (auth_len != RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN) {
2092                 DEBUG(0,("Incorrect auth_len %u.\n", (unsigned int)auth_len ));
2093                 return False;
2094         }
2095
2096         /*
2097          * The following is that length of the data we must verify or unseal.
2098          * This doesn't include the RPC headers or the auth_len or the RPC_HDR_AUTH_LEN
2099          * preceeding the auth_data.
2100          */
2101
2102         if (p->hdr.frag_len < RPC_HEADER_LEN + RPC_HDR_REQ_LEN + RPC_HDR_AUTH_LEN + auth_len) {
2103                 DEBUG(0,("Incorrect frag %u, auth %u.\n",
2104                         (unsigned int)p->hdr.frag_len,
2105                         (unsigned int)auth_len ));
2106                 return False;
2107         }
2108
2109         data_len = p->hdr.frag_len - RPC_HEADER_LEN - RPC_HDR_REQ_LEN - 
2110                 RPC_HDR_AUTH_LEN - auth_len;
2111         
2112         DEBUG(5,("data %d auth %d\n", data_len, auth_len));
2113
2114         if(!prs_set_offset(rpc_in, RPC_HDR_REQ_LEN + data_len)) {
2115                 DEBUG(0,("cannot move offset to %u.\n",
2116                          (unsigned int)RPC_HDR_REQ_LEN + data_len ));
2117                 return False;
2118         }
2119
2120         if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info, rpc_in, 0)) {
2121                 DEBUG(0,("failed to unmarshall RPC_HDR_AUTH.\n"));
2122                 return False;
2123         }
2124
2125         if (auth_info.auth_type != RPC_SCHANNEL_AUTH_TYPE) {
2126                 DEBUG(0,("Invalid auth info %d on schannel\n",
2127                          auth_info.auth_type));
2128                 return False;
2129         }
2130
2131         if(!smb_io_rpc_auth_schannel_chk("", RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN, &schannel_chk, rpc_in, 0)) {
2132                 DEBUG(0,("failed to unmarshal RPC_AUTH_SCHANNEL_CHK.\n"));
2133                 return False;
2134         }
2135
2136         if (!schannel_decode(p->auth.a_u.schannel_auth,
2137                            p->auth.auth_level,
2138                            SENDER_IS_INITIATOR,
2139                            &schannel_chk,
2140                            prs_data_p(rpc_in)+RPC_HDR_REQ_LEN, data_len)) {
2141                 DEBUG(3,("failed to decode PDU\n"));
2142                 return False;
2143         }
2144
2145         /*
2146          * Return the current pointer to the data offset.
2147          */
2148
2149         if(!prs_set_offset(rpc_in, save_offset)) {
2150                 DEBUG(0,("failed to set offset back to %u\n",
2151                          (unsigned int)save_offset ));
2152                 return False;
2153         }
2154
2155         /* The sequence number gets incremented on both send and receive. */
2156         p->auth.a_u.schannel_auth->seq_num++;
2157
2158         /*
2159          * Remember the padding length. We must remove it from the real data
2160          * stream once the sign/seal is done.
2161          */
2162
2163         *p_ss_padding_len = auth_info.auth_pad_len;
2164
2165         return True;
2166 }
2167
2168 /****************************************************************************
2169  Return a user struct for a pipe user.
2170 ****************************************************************************/
2171
2172 struct current_user *get_current_user(struct current_user *user, pipes_struct *p)
2173 {
2174         if (p->pipe_bound &&
2175                         (p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP ||
2176                         (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP))) {
2177                 memcpy(user, &p->pipe_user, sizeof(struct current_user));
2178         } else {
2179                 memcpy(user, &current_user, sizeof(struct current_user));
2180         }
2181
2182         return user;
2183 }
2184
2185 /****************************************************************************
2186  Find the set of RPC functions associated with this context_id
2187 ****************************************************************************/
2188
2189 static PIPE_RPC_FNS* find_pipe_fns_by_context( PIPE_RPC_FNS *list, uint32 context_id )
2190 {
2191         PIPE_RPC_FNS *fns = NULL;
2192         PIPE_RPC_FNS *tmp = NULL;
2193         
2194         if ( !list ) {
2195                 DEBUG(0,("find_pipe_fns_by_context: ERROR!  No context list for pipe!\n"));
2196                 return NULL;
2197         }
2198         
2199         for (tmp=list; tmp; tmp=tmp->next ) {
2200                 if ( tmp->context_id == context_id )
2201                         break;
2202         }
2203         
2204         fns = tmp;
2205         
2206         return fns;
2207 }
2208
2209 /****************************************************************************
2210  Memory cleanup.
2211 ****************************************************************************/
2212
2213 void free_pipe_rpc_context( PIPE_RPC_FNS *list )
2214 {
2215         PIPE_RPC_FNS *tmp = list;
2216         PIPE_RPC_FNS *tmp2;
2217                 
2218         while (tmp) {
2219                 tmp2 = tmp->next;
2220                 SAFE_FREE(tmp);
2221                 tmp = tmp2;
2222         }
2223
2224         return; 
2225 }
2226
2227 /****************************************************************************
2228  Find the correct RPC function to call for this request.
2229  If the pipe is authenticated then become the correct UNIX user
2230  before doing the call.
2231 ****************************************************************************/
2232
2233 bool api_pipe_request(pipes_struct *p)
2234 {
2235         bool ret = False;
2236         bool changed_user = False;
2237         PIPE_RPC_FNS *pipe_fns;
2238         
2239         if (p->pipe_bound &&
2240                         ((p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP) ||
2241                          (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP))) {
2242                 if(!become_authenticated_pipe_user(p)) {
2243                         prs_mem_free(&p->out_data.rdata);
2244                         return False;
2245                 }
2246                 changed_user = True;
2247         }
2248
2249         DEBUG(5, ("Requested \\PIPE\\%s\n", p->name));
2250         
2251         /* get the set of RPC functions for this context */
2252         
2253         pipe_fns = find_pipe_fns_by_context(p->contexts, p->hdr_req.context_id);
2254         
2255         if ( pipe_fns ) {
2256                 TALLOC_CTX *frame = talloc_stackframe();
2257                 ret = api_rpcTNP(p, p->name, pipe_fns->cmds, pipe_fns->n_cmds);
2258                 TALLOC_FREE(frame);
2259         }
2260         else {
2261                 DEBUG(0,("api_pipe_request: No rpc function table associated with context [%d] on pipe [%s]\n",
2262                         p->hdr_req.context_id, p->name));
2263         }
2264
2265         if (changed_user) {
2266                 unbecome_authenticated_pipe_user();
2267         }
2268
2269         return ret;
2270 }
2271
2272 /*******************************************************************
2273  Calls the underlying RPC function for a named pipe.
2274  ********************************************************************/
2275
2276 bool api_rpcTNP(pipes_struct *p, const char *rpc_name, 
2277                 const struct api_struct *api_rpc_cmds, int n_cmds)
2278 {
2279         int fn_num;
2280         fstring name;
2281         uint32 offset1, offset2;
2282  
2283         /* interpret the command */
2284         DEBUG(4,("api_rpcTNP: %s op 0x%x - ", rpc_name, p->hdr_req.opnum));
2285
2286         slprintf(name, sizeof(name)-1, "in_%s", rpc_name);
2287         prs_dump(name, p->hdr_req.opnum, &p->in_data.data);
2288
2289         for (fn_num = 0; fn_num < n_cmds; fn_num++) {
2290                 if (api_rpc_cmds[fn_num].opnum == p->hdr_req.opnum && api_rpc_cmds[fn_num].fn != NULL) {
2291                         DEBUG(3,("api_rpcTNP: rpc command: %s\n", api_rpc_cmds[fn_num].name));
2292                         break;
2293                 }
2294         }
2295
2296         if (fn_num == n_cmds) {
2297                 /*
2298                  * For an unknown RPC just return a fault PDU but
2299                  * return True to allow RPC's on the pipe to continue
2300                  * and not put the pipe into fault state. JRA.
2301                  */
2302                 DEBUG(4, ("unknown\n"));
2303                 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
2304                 return True;
2305         }
2306
2307         offset1 = prs_offset(&p->out_data.rdata);
2308
2309         DEBUG(6, ("api_rpc_cmds[%d].fn == %p\n", 
2310                 fn_num, api_rpc_cmds[fn_num].fn));
2311         /* do the actual command */
2312         if(!api_rpc_cmds[fn_num].fn(p)) {
2313                 DEBUG(0,("api_rpcTNP: %s: %s failed.\n", rpc_name, api_rpc_cmds[fn_num].name));
2314                 prs_mem_free(&p->out_data.rdata);
2315                 return False;
2316         }
2317
2318         if (p->bad_handle_fault_state) {
2319                 DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
2320                 p->bad_handle_fault_state = False;
2321                 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_CONTEXT_MISMATCH));
2322                 return True;
2323         }
2324
2325         if (p->rng_fault_state) {
2326                 DEBUG(4, ("api_rpcTNP: rng fault return\n"));
2327                 p->rng_fault_state = False;
2328                 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
2329                 return True;
2330         }
2331
2332         slprintf(name, sizeof(name)-1, "out_%s", rpc_name);
2333         offset2 = prs_offset(&p->out_data.rdata);
2334         prs_set_offset(&p->out_data.rdata, offset1);
2335         prs_dump(name, p->hdr_req.opnum, &p->out_data.rdata);
2336         prs_set_offset(&p->out_data.rdata, offset2);
2337
2338         DEBUG(5,("api_rpcTNP: called %s successfully\n", rpc_name));
2339
2340         /* Check for buffer underflow in rpc parsing */
2341
2342         if ((DEBUGLEVEL >= 10) && 
2343             (prs_offset(&p->in_data.data) != prs_data_size(&p->in_data.data))) {
2344                 size_t data_len = prs_data_size(&p->in_data.data) - prs_offset(&p->in_data.data);
2345                 char *data = (char *)SMB_MALLOC(data_len);
2346
2347                 DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
2348                 if (data) {
2349                         prs_uint8s(False, "", &p->in_data.data, 0, (unsigned char *)data, (uint32)data_len);
2350                         SAFE_FREE(data);
2351                 }
2352
2353         }
2354
2355         return True;
2356 }
2357
2358 /*******************************************************************
2359 *******************************************************************/
2360
2361 void get_pipe_fns( int idx, struct api_struct **fns, int *n_fns )
2362 {
2363         struct api_struct *cmds = NULL;
2364         int               n_cmds = 0;
2365
2366         switch ( idx ) {
2367                 case PI_LSARPC:
2368                         lsa_get_pipe_fns( &cmds, &n_cmds );
2369                         break;
2370                 case PI_LSARPC_DS:
2371                         lsa_ds_get_pipe_fns( &cmds, &n_cmds );
2372                         break;
2373                 case PI_SAMR:
2374                         samr_get_pipe_fns( &cmds, &n_cmds );
2375                         break;
2376                 case PI_NETLOGON:
2377                         netlog_get_pipe_fns( &cmds, &n_cmds );
2378                         break;
2379                 case PI_SRVSVC:
2380                         srvsvc2_get_pipe_fns( &cmds, &n_cmds );
2381                         break;
2382                 case PI_WKSSVC:
2383                         wkssvc_get_pipe_fns( &cmds, &n_cmds );
2384                         break;
2385                 case PI_WINREG:
2386                         winreg_get_pipe_fns( &cmds, &n_cmds );
2387                         break;
2388                 case PI_SPOOLSS:
2389                         spoolss_get_pipe_fns( &cmds, &n_cmds );
2390                         break;
2391                 case PI_NETDFS:
2392                         netdfs_get_pipe_fns( &cmds, &n_cmds );
2393                         break;
2394                 case PI_SVCCTL:
2395                         svcctl2_get_pipe_fns( &cmds, &n_cmds );
2396                         break;
2397                 case PI_EVENTLOG:
2398                         eventlog2_get_pipe_fns( &cmds, &n_cmds );
2399                         break;
2400                 case PI_NTSVCS:
2401                         ntsvcs_get_pipe_fns( &cmds, &n_cmds );
2402                         break;
2403 #ifdef DEVELOPER
2404                 case PI_RPCECHO:
2405                         rpcecho_get_pipe_fns( &cmds, &n_cmds );
2406                         break;
2407 #endif
2408                 default:
2409                         DEBUG(0,("get_pipe_fns: Unknown pipe index! [%d]\n", idx));
2410         }
2411
2412         *fns = cmds;
2413         *n_fns = n_cmds;
2414
2415         return;
2416 }