2 * Unix SMB/Netbios implementation.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-1998
6 * Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
7 * Copyright (C) Paul Ashton 1997-1998.
8 * Copyright (C) Jeremy Allison 1999.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* this module apparently provides an implementation of DCE/RPC over a
26 * named pipe (IPC$ connection using SMBtrans). details of DCE/RPC
27 * documentation are available (in on-line form) from the X-Open group.
29 * this module should provide a level of abstraction between SMB
30 * and DCE/RPC, while minimising the amount of mallocs, unnecessary
31 * data copies, and network traffic.
33 * in this version, which takes a "let's learn what's going on and
34 * get something running" approach, there is additional network
35 * traffic generated, but the code should be easier to understand...
37 * ... if you read the docs. or stare at packets for weeks on end.
43 static void NTLMSSPcalc_p( pipes_struct
*p
, unsigned char *data
, int len
)
45 unsigned char *hash
= p
->ntlmssp_hash
;
46 unsigned char index_i
= hash
[256];
47 unsigned char index_j
= hash
[257];
50 for( ind
= 0; ind
< len
; ind
++) {
55 index_j
+= hash
[index_i
];
58 hash
[index_i
] = hash
[index_j
];
61 t
= hash
[index_i
] + hash
[index_j
];
62 data
[ind
] = data
[ind
] ^ hash
[t
];
69 /*******************************************************************
70 Generate the next PDU to be returned from the data in p->rdata.
71 We cheat here as this function doesn't handle the special auth
72 footers of the authenticated bind response reply.
73 ********************************************************************/
75 BOOL
create_next_pdu(pipes_struct
*p
)
77 RPC_HDR_RESP hdr_resp
;
78 BOOL auth_verify
= ((p
->ntlmssp_chal_flags
& NTLMSSP_NEGOTIATE_SIGN
) != 0);
79 BOOL auth_seal
= ((p
->ntlmssp_chal_flags
& NTLMSSP_NEGOTIATE_SEAL
) != 0);
80 uint32 ss_padding_len
= 0;
82 uint32 data_space_available
;
84 prs_struct outgoing_pdu
;
90 * If we're in the fault state, keep returning fault PDU's until
91 * the pipe gets closed. JRA.
95 setup_fault_pdu(p
, NT_STATUS(0x1c010002));
99 memset((char *)&hdr_resp
, '\0', sizeof(hdr_resp
));
101 /* Change the incoming request header to a response. */
102 p
->hdr
.pkt_type
= RPC_RESPONSE
;
104 /* Set up rpc header flags. */
105 if (p
->out_data
.data_sent_length
== 0)
106 p
->hdr
.flags
= RPC_FLG_FIRST
;
111 * Work out how much we can fit in a single PDU.
114 data_space_available
= sizeof(p
->out_data
.current_pdu
) - RPC_HEADER_LEN
- RPC_HDR_RESP_LEN
;
115 if(p
->ntlmssp_auth_validated
)
116 data_space_available
-= (RPC_HDR_AUTH_LEN
+ RPC_AUTH_NTLMSSP_CHK_LEN
);
119 * The amount we send is the minimum of the available
120 * space and the amount left to send.
123 data_len_left
= prs_offset(&p
->out_data
.rdata
) - p
->out_data
.data_sent_length
;
126 * Ensure there really is data left to send.
130 DEBUG(0,("create_next_pdu: no data left to send !\n"));
134 data_len
= MIN(data_len_left
, data_space_available
);
137 * Set up the alloc hint. This should be the data left to
141 hdr_resp
.alloc_hint
= data_len_left
;
144 * Work out if this PDU will be the last.
147 if(p
->out_data
.data_sent_length
+ data_len
>= prs_offset(&p
->out_data
.rdata
)) {
148 p
->hdr
.flags
|= RPC_FLG_LAST
;
149 if ((auth_seal
|| auth_verify
) && (data_len_left
% 8)) {
150 ss_padding_len
= 8 - (data_len_left
% 8);
151 DEBUG(10,("create_next_pdu: adding sign/seal padding of %u\n",
157 * Set up the header lengths.
160 if (p
->ntlmssp_auth_validated
) {
161 p
->hdr
.frag_len
= RPC_HEADER_LEN
+ RPC_HDR_RESP_LEN
+ data_len
+ ss_padding_len
+
162 RPC_HDR_AUTH_LEN
+ RPC_AUTH_NTLMSSP_CHK_LEN
;
163 p
->hdr
.auth_len
= RPC_AUTH_NTLMSSP_CHK_LEN
;
165 p
->hdr
.frag_len
= RPC_HEADER_LEN
+ RPC_HDR_RESP_LEN
+ data_len
;
170 * Init the parse struct to point at the outgoing
174 prs_init( &outgoing_pdu
, 0, p
->mem_ctx
, MARSHALL
);
175 prs_give_memory( &outgoing_pdu
, (char *)p
->out_data
.current_pdu
, sizeof(p
->out_data
.current_pdu
), False
);
177 /* Store the header in the data stream. */
178 if(!smb_io_rpc_hdr("hdr", &p
->hdr
, &outgoing_pdu
, 0)) {
179 DEBUG(0,("create_next_pdu: failed to marshall RPC_HDR.\n"));
180 prs_mem_free(&outgoing_pdu
);
184 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp
, &outgoing_pdu
, 0)) {
185 DEBUG(0,("create_next_pdu: failed to marshall RPC_HDR_RESP.\n"));
186 prs_mem_free(&outgoing_pdu
);
190 /* Store the current offset. */
191 data_pos
= prs_offset(&outgoing_pdu
);
193 /* Copy the data into the PDU. */
194 data_from
= prs_data_p(&p
->out_data
.rdata
) + p
->out_data
.data_sent_length
;
196 if(!prs_append_data(&outgoing_pdu
, data_from
, data_len
)) {
197 DEBUG(0,("create_next_pdu: failed to copy %u bytes of data.\n", (unsigned int)data_len
));
198 prs_mem_free(&outgoing_pdu
);
202 /* Copy the sign/seal padding data. */
203 if (ss_padding_len
) {
205 memset(pad
, '\0', 8);
206 if (!prs_append_data(&outgoing_pdu
, pad
, ss_padding_len
)) {
207 DEBUG(0,("create_next_pdu: failed to add %u bytes of pad data.\n", (unsigned int)ss_padding_len
));
208 prs_mem_free(&outgoing_pdu
);
214 * Set data to point to where we copied the data into.
217 data
= prs_data_p(&outgoing_pdu
) + data_pos
;
219 if (p
->hdr
.auth_len
> 0) {
222 DEBUG(5,("create_next_pdu: sign: %s seal: %s data %d auth %d\n",
223 BOOLSTR(auth_verify
), BOOLSTR(auth_seal
), data_len
+ ss_padding_len
, p
->hdr
.auth_len
));
226 crc32
= crc32_calc_buffer(data
, data_len
+ ss_padding_len
);
227 NTLMSSPcalc_p(p
, (uchar
*)data
, data_len
+ ss_padding_len
);
230 if (auth_seal
|| auth_verify
) {
231 RPC_HDR_AUTH auth_info
;
233 init_rpc_hdr_auth(&auth_info
, NTLMSSP_AUTH_TYPE
, NTLMSSP_AUTH_LEVEL
,
234 (auth_verify
? ss_padding_len
: 0), (auth_verify
? 1 : 0));
235 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info
, &outgoing_pdu
, 0)) {
236 DEBUG(0,("create_next_pdu: failed to marshall RPC_HDR_AUTH.\n"));
237 prs_mem_free(&outgoing_pdu
);
243 RPC_AUTH_NTLMSSP_CHK ntlmssp_chk
;
244 char *auth_data
= prs_data_p(&outgoing_pdu
);
246 p
->ntlmssp_seq_num
++;
247 init_rpc_auth_ntlmssp_chk(&ntlmssp_chk
, NTLMSSP_SIGN_VERSION
,
248 crc32
, p
->ntlmssp_seq_num
++);
249 auth_data
= prs_data_p(&outgoing_pdu
) + prs_offset(&outgoing_pdu
) + 4;
250 if(!smb_io_rpc_auth_ntlmssp_chk("auth_sign", &ntlmssp_chk
, &outgoing_pdu
, 0)) {
251 DEBUG(0,("create_next_pdu: failed to marshall RPC_AUTH_NTLMSSP_CHK.\n"));
252 prs_mem_free(&outgoing_pdu
);
255 NTLMSSPcalc_p(p
, (uchar
*)auth_data
, RPC_AUTH_NTLMSSP_CHK_LEN
- 4);
260 * Setup the counts for this PDU.
263 p
->out_data
.data_sent_length
+= data_len
;
264 p
->out_data
.current_pdu_len
= p
->hdr
.frag_len
;
265 p
->out_data
.current_pdu_sent
= 0;
267 prs_mem_free(&outgoing_pdu
);
271 /*******************************************************************
272 Process an NTLMSSP authentication response.
273 If this function succeeds, the user has been authenticated
274 and their domain, name and calling workstation stored in
276 The initial challenge is stored in p->challenge.
277 *******************************************************************/
279 static BOOL
api_pipe_ntlmssp_verify(pipes_struct
*p
, RPC_AUTH_NTLMSSP_RESP
*ntlmssp_resp
)
284 fstring pipe_user_name
;
287 BOOL guest_user
= False
;
288 SAM_ACCOUNT
*sampass
= NULL
;
289 uchar null_smb_passwd
[16];
290 uchar
*smb_passwd_ptr
= NULL
;
292 DEBUG(5,("api_pipe_ntlmssp_verify: checking user details\n"));
294 memset(p
->user_name
, '\0', sizeof(p
->user_name
));
295 memset(p
->pipe_user_name
, '\0', sizeof(p
->pipe_user_name
));
296 memset(p
->domain
, '\0', sizeof(p
->domain
));
297 memset(p
->wks
, '\0', sizeof(p
->wks
));
299 /* Set up for non-authenticated user. */
300 delete_nt_token(&p
->pipe_user
.nt_user_token
);
301 p
->pipe_user
.ngroups
= 0;
302 safe_free( p
->pipe_user
.groups
);
305 * Setup an empty password for a guest user.
308 memset(null_smb_passwd
,0,16);
311 * We always negotiate UNICODE.
314 if (p
->ntlmssp_chal_flags
& NTLMSSP_NEGOTIATE_UNICODE
) {
315 fstrcpy(user_name
, dos_unistrn2((uint16
*)ntlmssp_resp
->user
, ntlmssp_resp
->hdr_usr
.str_str_len
/2));
316 fstrcpy(domain
, dos_unistrn2((uint16
*)ntlmssp_resp
->domain
, ntlmssp_resp
->hdr_domain
.str_str_len
/2));
317 fstrcpy(wks
, dos_unistrn2((uint16
*)ntlmssp_resp
->wks
, ntlmssp_resp
->hdr_wks
.str_str_len
/2));
319 fstrcpy(user_name
, ntlmssp_resp
->user
);
320 fstrcpy(domain
, ntlmssp_resp
->domain
);
321 fstrcpy(wks
, ntlmssp_resp
->wks
);
324 DEBUG(5,("user: %s domain: %s wks: %s\n", user_name
, domain
, wks
));
326 memcpy(lm_owf
, ntlmssp_resp
->lm_resp
, sizeof(lm_owf
));
327 memcpy(nt_owf
, ntlmssp_resp
->nt_resp
, sizeof(nt_owf
));
329 #ifdef DEBUG_PASSWORD
330 DEBUG(100,("lm, nt owfs, chal\n"));
331 dump_data(100, (char *)lm_owf
, sizeof(lm_owf
));
332 dump_data(100, (char *)nt_owf
, sizeof(nt_owf
));
333 dump_data(100, (char *)p
->challenge
, 8);
337 * Allow guest access. Patch from Shirish Kalele <kalele@veritas.com>.
340 if((strlen(user_name
) == 0) &&
341 (ntlmssp_resp
->hdr_nt_resp
.str_str_len
==0))
345 fstrcpy(pipe_user_name
, lp_guestaccount(-1));
346 DEBUG(100,("Null user in NTLMSSP verification. Using guest = %s\n", pipe_user_name
));
348 smb_passwd_ptr
= null_smb_passwd
;
353 * Pass the user through the NT -> unix user mapping
357 fstrcpy(pipe_user_name
, user_name
);
358 (void)map_username(pipe_user_name
);
361 * Do the length checking only if user is not NULL.
364 if (ntlmssp_resp
->hdr_lm_resp
.str_str_len
== 0)
366 if (ntlmssp_resp
->hdr_nt_resp
.str_str_len
== 0)
368 if (ntlmssp_resp
->hdr_usr
.str_str_len
== 0)
370 if (ntlmssp_resp
->hdr_domain
.str_str_len
== 0)
372 if (ntlmssp_resp
->hdr_wks
.str_str_len
== 0)
381 if(!(p
->ntlmssp_auth_validated
= pass_check_smb(pipe_user_name
, domain
,
382 (uchar
*)p
->challenge
, lm_owf
, nt_owf
, NULL
))) {
383 DEBUG(1,("api_pipe_ntlmssp_verify: User %s\\%s from machine %s \
384 failed authentication on named pipe %s.\n", domain
, pipe_user_name
, wks
, p
->name
));
389 pdb_init_sam(&sampass
);
391 if(!pdb_getsampwnam(sampass
, pipe_user_name
)) {
392 DEBUG(1,("api_pipe_ntlmssp_verify: Cannot find user %s in smb passwd database.\n",
394 pdb_free_sam(sampass
);
401 /* Quit if the account was disabled. */
402 if((pdb_get_acct_ctrl(sampass
) & ACB_DISABLED
) || !pdb_get_lanman_passwd(sampass
)) {
403 DEBUG(1,("Account for user '%s' was disabled.\n", pipe_user_name
));
404 pdb_free_sam(sampass
);
408 if(!pdb_get_nt_passwd(sampass
)) {
409 DEBUG(1,("Account for user '%s' has no NT password hash.\n", pipe_user_name
));
410 pdb_free_sam(sampass
);
414 smb_passwd_ptr
= pdb_get_lanman_passwd(sampass
);
417 * Store the UNIX credential data (uid/gid pair) in the pipe structure.
420 p
->pipe_user
.uid
= pdb_get_uid(sampass
);
421 p
->pipe_user
.gid
= pdb_get_gid(sampass
);
426 /* setup the guest credentials; assuming that the guest
427 account does not have to exist in the smbpasswd file */
429 if ( (pw
= sys_getpwnam(pipe_user_name
)) == NULL
) {
430 DEBUG(0,("api_pipe_ntlmssp_verify: Invalid guest account [%s]. getpwnam() failed!\n",
432 pdb_free_sam(sampass
);
436 p
->pipe_user
.uid
= pw
->pw_uid
;
437 p
->pipe_user
.gid
= pw
->pw_gid
;
442 * Set up the sign/seal data.
447 NTLMSSPOWFencrypt(smb_passwd_ptr
, lm_owf
, p24
);
459 for (ind
= 0; ind
< 256; ind
++)
460 p
->ntlmssp_hash
[ind
] = (unsigned char)ind
;
462 for( ind
= 0; ind
< 256; ind
++) {
465 j
+= (p
->ntlmssp_hash
[ind
] + k2
[ind
%8]);
467 tc
= p
->ntlmssp_hash
[ind
];
468 p
->ntlmssp_hash
[ind
] = p
->ntlmssp_hash
[j
];
469 p
->ntlmssp_hash
[j
] = tc
;
472 p
->ntlmssp_hash
[256] = 0;
473 p
->ntlmssp_hash
[257] = 0;
475 /* NTLMSSPhash(p->ntlmssp_hash, p24); */
476 p
->ntlmssp_seq_num
= 0;
480 fstrcpy(p
->user_name
, user_name
);
481 fstrcpy(p
->pipe_user_name
, pipe_user_name
);
482 fstrcpy(p
->domain
, domain
);
483 fstrcpy(p
->wks
, wks
);
485 /* Set up pipe user group membership. */
486 initialise_groups(pipe_user_name
, p
->pipe_user
.uid
, p
->pipe_user
.gid
);
487 get_current_groups( p
->pipe_user
.gid
, &p
->pipe_user
.ngroups
, &p
->pipe_user
.groups
);
489 /* Create an NT_USER_TOKEN struct for this user. */
490 p
->pipe_user
.nt_user_token
= create_nt_token(p
->pipe_user
.uid
,p
->pipe_user
.gid
,
491 p
->pipe_user
.ngroups
, p
->pipe_user
.groups
,
494 p
->ntlmssp_auth_validated
= True
;
495 pdb_free_sam(sampass
);
499 /*******************************************************************
500 The switch table for the pipe names and the functions to handle them.
501 *******************************************************************/
505 const char * pipe_clnt_name
;
506 const char * pipe_srv_name
;
507 BOOL (*fn
) (pipes_struct
*);
510 static struct api_cmd api_fd_commands
[] =
512 { "lsarpc", "lsass", api_ntlsa_rpc
},
513 { "samr", "lsass", api_samr_rpc
},
514 { "srvsvc", "ntsvcs", api_srvsvc_rpc
},
515 { "wkssvc", "ntsvcs", api_wkssvc_rpc
},
516 { "NETLOGON", "lsass", api_netlog_rpc
},
517 { "winreg", "winreg", api_reg_rpc
},
518 { "spoolss", "spoolss", api_spoolss_rpc
},
520 { "netdfs", "netdfs" , api_netdfs_rpc
},
525 /*******************************************************************
526 This is the client reply to our challenge for an authenticated
527 bind request. The challenge we sent is in p->challenge.
528 *******************************************************************/
530 BOOL
api_pipe_bind_auth_resp(pipes_struct
*p
, prs_struct
*rpc_in_p
)
532 RPC_HDR_AUTHA autha_info
;
533 RPC_AUTH_VERIFIER auth_verifier
;
534 RPC_AUTH_NTLMSSP_RESP ntlmssp_resp
;
536 DEBUG(5,("api_pipe_bind_auth_resp: decode request. %d\n", __LINE__
));
538 if (p
->hdr
.auth_len
== 0) {
539 DEBUG(0,("api_pipe_bind_auth_resp: No auth field sent !\n"));
544 * Decode the authentication verifier response.
547 if(!smb_io_rpc_hdr_autha("", &autha_info
, rpc_in_p
, 0)) {
548 DEBUG(0,("api_pipe_bind_auth_resp: unmarshall of RPC_HDR_AUTHA failed.\n"));
552 if (autha_info
.auth_type
!= NTLMSSP_AUTH_TYPE
|| autha_info
.auth_level
!= NTLMSSP_AUTH_LEVEL
) {
553 DEBUG(0,("api_pipe_bind_auth_resp: incorrect auth type (%d) or level (%d).\n",
554 (int)autha_info
.auth_type
, (int)autha_info
.auth_level
));
558 if(!smb_io_rpc_auth_verifier("", &auth_verifier
, rpc_in_p
, 0)) {
559 DEBUG(0,("api_pipe_bind_auth_resp: unmarshall of RPC_AUTH_VERIFIER failed.\n"));
564 * Ensure this is a NTLMSSP_AUTH packet type.
567 if (!rpc_auth_verifier_chk(&auth_verifier
, "NTLMSSP", NTLMSSP_AUTH
)) {
568 DEBUG(0,("api_pipe_bind_auth_resp: rpc_auth_verifier_chk failed.\n"));
572 if(!smb_io_rpc_auth_ntlmssp_resp("", &ntlmssp_resp
, rpc_in_p
, 0)) {
573 DEBUG(0,("api_pipe_bind_auth_resp: Failed to unmarshall RPC_AUTH_NTLMSSP_RESP.\n"));
578 * The following call actually checks the challenge/response data.
579 * for correctness against the given DOMAIN\user name.
582 if (!api_pipe_ntlmssp_verify(p
, &ntlmssp_resp
))
590 /*******************************************************************
591 Marshall a bind_nak pdu.
592 *******************************************************************/
594 static BOOL
setup_bind_nak(pipes_struct
*p
)
596 prs_struct outgoing_rpc
;
600 /* Free any memory in the current return data buffer. */
601 prs_mem_free(&p
->out_data
.rdata
);
604 * Marshall directly into the outgoing PDU space. We
605 * must do this as we need to set to the bind response
606 * header and are never sending more than one PDU here.
609 prs_init( &outgoing_rpc
, 0, p
->mem_ctx
, MARSHALL
);
610 prs_give_memory( &outgoing_rpc
, (char *)p
->out_data
.current_pdu
, sizeof(p
->out_data
.current_pdu
), False
);
614 * Initialize a bind_nak header.
617 init_rpc_hdr(&nak_hdr
, RPC_BINDNACK
, RPC_FLG_FIRST
| RPC_FLG_LAST
,
618 p
->hdr
.call_id
, RPC_HEADER_LEN
+ sizeof(uint16
), 0);
621 * Marshall the header into the outgoing PDU.
624 if(!smb_io_rpc_hdr("", &nak_hdr
, &outgoing_rpc
, 0)) {
625 DEBUG(0,("setup_bind_nak: marshalling of RPC_HDR failed.\n"));
626 prs_mem_free(&outgoing_rpc
);
631 * Now add the reject reason.
634 if(!prs_uint16("reject code", &outgoing_rpc
, 0, &zero
)) {
635 prs_mem_free(&outgoing_rpc
);
639 p
->out_data
.data_sent_length
= 0;
640 p
->out_data
.current_pdu_len
= prs_offset(&outgoing_rpc
);
641 p
->out_data
.current_pdu_sent
= 0;
643 p
->pipe_bound
= False
;
648 /*******************************************************************
649 Marshall a fault pdu.
650 *******************************************************************/
652 BOOL
setup_fault_pdu(pipes_struct
*p
, NTSTATUS status
)
654 prs_struct outgoing_pdu
;
656 RPC_HDR_RESP hdr_resp
;
657 RPC_HDR_FAULT fault_resp
;
659 /* Free any memory in the current return data buffer. */
660 prs_mem_free(&p
->out_data
.rdata
);
663 * Marshall directly into the outgoing PDU space. We
664 * must do this as we need to set to the bind response
665 * header and are never sending more than one PDU here.
668 prs_init( &outgoing_pdu
, 0, p
->mem_ctx
, MARSHALL
);
669 prs_give_memory( &outgoing_pdu
, (char *)p
->out_data
.current_pdu
, sizeof(p
->out_data
.current_pdu
), False
);
672 * Initialize a fault header.
675 init_rpc_hdr(&fault_hdr
, RPC_FAULT
, RPC_FLG_FIRST
| RPC_FLG_LAST
| RPC_FLG_NOCALL
,
676 p
->hdr
.call_id
, RPC_HEADER_LEN
+ RPC_HDR_RESP_LEN
+ RPC_HDR_FAULT_LEN
, 0);
679 * Initialize the HDR_RESP and FAULT parts of the PDU.
682 memset((char *)&hdr_resp
, '\0', sizeof(hdr_resp
));
684 fault_resp
.status
= status
;
685 fault_resp
.reserved
= 0;
688 * Marshall the header into the outgoing PDU.
691 if(!smb_io_rpc_hdr("", &fault_hdr
, &outgoing_pdu
, 0)) {
692 DEBUG(0,("setup_fault_pdu: marshalling of RPC_HDR failed.\n"));
693 prs_mem_free(&outgoing_pdu
);
697 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp
, &outgoing_pdu
, 0)) {
698 DEBUG(0,("setup_fault_pdu: failed to marshall RPC_HDR_RESP.\n"));
699 prs_mem_free(&outgoing_pdu
);
703 if(!smb_io_rpc_hdr_fault("fault", &fault_resp
, &outgoing_pdu
, 0)) {
704 DEBUG(0,("setup_fault_pdu: failed to marshall RPC_HDR_FAULT.\n"));
705 prs_mem_free(&outgoing_pdu
);
709 p
->out_data
.data_sent_length
= 0;
710 p
->out_data
.current_pdu_len
= prs_offset(&outgoing_pdu
);
711 p
->out_data
.current_pdu_sent
= 0;
713 prs_mem_free(&outgoing_pdu
);
717 /*******************************************************************
718 Ensure a bind request has the correct abstract & transfer interface.
719 Used to reject unknown binds from Win2k.
720 *******************************************************************/
722 BOOL
check_bind_req(char* pipe_name
, RPC_IFACE
* abstract
,
725 extern struct pipe_id_info pipe_names
[];
728 fstrcpy(pname
,"\\PIPE\\");
729 fstrcat(pname
,pipe_name
);
731 for(i
=0;pipe_names
[i
].client_pipe
; i
++) {
732 if(strequal(pipe_names
[i
].client_pipe
, pname
))
736 if(pipe_names
[i
].client_pipe
== NULL
)
739 /* check the abstract interface */
740 if((abstract
->version
!= pipe_names
[i
].abstr_syntax
.version
) ||
741 (memcmp(&abstract
->uuid
, &pipe_names
[i
].abstr_syntax
.uuid
,
742 sizeof(RPC_UUID
)) != 0))
745 /* check the transfer interface */
746 if((transfer
->version
!= pipe_names
[i
].trans_syntax
.version
) ||
747 (memcmp(&transfer
->uuid
, &pipe_names
[i
].trans_syntax
.uuid
,
748 sizeof(RPC_UUID
)) != 0))
754 /*******************************************************************
755 Respond to a pipe bind request.
756 *******************************************************************/
758 BOOL
api_pipe_bind_req(pipes_struct
*p
, prs_struct
*rpc_in_p
)
762 RPC_HDR_AUTH auth_info
;
764 fstring ack_pipe_name
;
765 prs_struct out_hdr_ba
;
767 prs_struct outgoing_rpc
;
770 enum RPC_PKT_TYPE reply_pkt_type
;
772 p
->ntlmssp_auth_requested
= False
;
774 DEBUG(5,("api_pipe_bind_req: decode request. %d\n", __LINE__
));
777 * Try and find the correct pipe name to ensure
778 * that this is a pipe name we support.
781 for (i
= 0; api_fd_commands
[i
].pipe_clnt_name
; i
++) {
782 if (strequal(api_fd_commands
[i
].pipe_clnt_name
, p
->name
) &&
783 api_fd_commands
[i
].fn
!= NULL
) {
784 DEBUG(3,("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
785 api_fd_commands
[i
].pipe_clnt_name
,
786 api_fd_commands
[i
].pipe_srv_name
));
787 fstrcpy(p
->pipe_srv_name
, api_fd_commands
[i
].pipe_srv_name
);
792 if (api_fd_commands
[i
].fn
== NULL
) {
793 DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
795 if(!setup_bind_nak(p
))
800 /* decode the bind request */
801 if(!smb_io_rpc_hdr_rb("", &hdr_rb
, rpc_in_p
, 0)) {
802 DEBUG(0,("api_pipe_bind_req: unable to unmarshall RPC_HDR_RB struct.\n"));
807 * Check if this is an authenticated request.
810 if (p
->hdr
.auth_len
!= 0) {
811 RPC_AUTH_VERIFIER auth_verifier
;
812 RPC_AUTH_NTLMSSP_NEG ntlmssp_neg
;
815 * Decode the authentication verifier.
818 if(!smb_io_rpc_hdr_auth("", &auth_info
, rpc_in_p
, 0)) {
819 DEBUG(0,("api_pipe_bind_req: unable to unmarshall RPC_HDR_AUTH struct.\n"));
824 * We only support NTLMSSP_AUTH_TYPE requests.
827 if(auth_info
.auth_type
!= NTLMSSP_AUTH_TYPE
) {
828 DEBUG(0,("api_pipe_bind_req: unknown auth type %x requested.\n",
829 auth_info
.auth_type
));
833 if(!smb_io_rpc_auth_verifier("", &auth_verifier
, rpc_in_p
, 0)) {
834 DEBUG(0,("api_pipe_bind_req: unable to unmarshall RPC_HDR_AUTH struct.\n"));
838 if(!strequal(auth_verifier
.signature
, "NTLMSSP")) {
839 DEBUG(0,("api_pipe_bind_req: auth_verifier.signature != NTLMSSP\n"));
843 if(auth_verifier
.msg_type
!= NTLMSSP_NEGOTIATE
) {
844 DEBUG(0,("api_pipe_bind_req: auth_verifier.msg_type (%d) != NTLMSSP_NEGOTIATE\n",
845 auth_verifier
.msg_type
));
849 if(!smb_io_rpc_auth_ntlmssp_neg("", &ntlmssp_neg
, rpc_in_p
, 0)) {
850 DEBUG(0,("api_pipe_bind_req: Failed to unmarshall RPC_AUTH_NTLMSSP_NEG.\n"));
854 p
->ntlmssp_chal_flags
= SMBD_NTLMSSP_NEG_FLAGS
;
855 p
->ntlmssp_auth_requested
= True
;
858 switch(p
->hdr
.pkt_type
) {
860 /* name has to be \PIPE\xxxxx */
861 fstrcpy(ack_pipe_name
, "\\PIPE\\");
862 fstrcat(ack_pipe_name
, p
->pipe_srv_name
);
863 reply_pkt_type
= RPC_BINDACK
;
866 /* secondary address CAN be NULL
867 * as the specs say it's ignored.
868 * It MUST NULL to have the spoolss working.
870 fstrcpy(ack_pipe_name
,"");
871 reply_pkt_type
= RPC_ALTCONTRESP
;
877 DEBUG(5,("api_pipe_bind_req: make response. %d\n", __LINE__
));
880 * Marshall directly into the outgoing PDU space. We
881 * must do this as we need to set to the bind response
882 * header and are never sending more than one PDU here.
885 prs_init( &outgoing_rpc
, 0, p
->mem_ctx
, MARSHALL
);
886 prs_give_memory( &outgoing_rpc
, (char *)p
->out_data
.current_pdu
, sizeof(p
->out_data
.current_pdu
), False
);
889 * Setup the memory to marshall the ba header, and the
893 if(!prs_init(&out_hdr_ba
, 1024, p
->mem_ctx
, MARSHALL
)) {
894 DEBUG(0,("api_pipe_bind_req: malloc out_hdr_ba failed.\n"));
895 prs_mem_free(&outgoing_rpc
);
899 if(!prs_init(&out_auth
, 1024, p
->mem_ctx
, MARSHALL
)) {
900 DEBUG(0,("pi_pipe_bind_req: malloc out_auth failed.\n"));
901 prs_mem_free(&outgoing_rpc
);
902 prs_mem_free(&out_hdr_ba
);
906 if (p
->ntlmssp_auth_requested
)
909 assoc_gid
= hdr_rb
.bba
.assoc_gid
? hdr_rb
.bba
.assoc_gid
: 0x53f0;
912 * Create the bind response struct.
915 /* If the requested abstract synt uuid doesn't match our client pipe,
916 reject the bind_ack & set the transfer interface synt to all 0's,
917 ver 0 (observed when NT5 attempts to bind to abstract interfaces
919 Needed when adding entries to a DACL from NT5 - SK */
921 if(check_bind_req(p
->name
, &hdr_rb
.abstract
, &hdr_rb
.transfer
)) {
922 init_rpc_hdr_ba(&hdr_ba
,
930 RPC_IFACE null_interface
;
931 ZERO_STRUCT(null_interface
);
932 /* Rejection reason: abstract syntax not supported */
933 init_rpc_hdr_ba(&hdr_ba
, MAX_PDU_FRAG_LEN
,
934 MAX_PDU_FRAG_LEN
, assoc_gid
,
935 ack_pipe_name
, 0x1, 0x2, 0x1,
943 if(!smb_io_rpc_hdr_ba("", &hdr_ba
, &out_hdr_ba
, 0)) {
944 DEBUG(0,("api_pipe_bind_req: marshalling of RPC_HDR_BA failed.\n"));
949 * Now the authentication.
952 if (p
->ntlmssp_auth_requested
) {
953 RPC_AUTH_VERIFIER auth_verifier
;
954 RPC_AUTH_NTLMSSP_CHAL ntlmssp_chal
;
956 generate_random_buffer(p
->challenge
, 8, False
);
958 /*** Authentication info ***/
960 init_rpc_hdr_auth(&auth_info
, NTLMSSP_AUTH_TYPE
, NTLMSSP_AUTH_LEVEL
, RPC_HDR_AUTH_LEN
, 1);
961 if(!smb_io_rpc_hdr_auth("", &auth_info
, &out_auth
, 0)) {
962 DEBUG(0,("api_pipe_bind_req: marshalling of RPC_HDR_AUTH failed.\n"));
966 /*** NTLMSSP verifier ***/
968 init_rpc_auth_verifier(&auth_verifier
, "NTLMSSP", NTLMSSP_CHALLENGE
);
969 if(!smb_io_rpc_auth_verifier("", &auth_verifier
, &out_auth
, 0)) {
970 DEBUG(0,("api_pipe_bind_req: marshalling of RPC_AUTH_VERIFIER failed.\n"));
974 /* NTLMSSP challenge ***/
976 init_rpc_auth_ntlmssp_chal(&ntlmssp_chal
, p
->ntlmssp_chal_flags
, p
->challenge
);
977 if(!smb_io_rpc_auth_ntlmssp_chal("", &ntlmssp_chal
, &out_auth
, 0)) {
978 DEBUG(0,("api_pipe_bind_req: marshalling of RPC_AUTH_NTLMSSP_CHAL failed.\n"));
982 /* Auth len in the rpc header doesn't include auth_header. */
983 auth_len
= prs_offset(&out_auth
) - RPC_HDR_AUTH_LEN
;
987 * Create the header, now we know the length.
990 init_rpc_hdr(&p
->hdr
, reply_pkt_type
, RPC_FLG_FIRST
| RPC_FLG_LAST
,
992 RPC_HEADER_LEN
+ prs_offset(&out_hdr_ba
) + prs_offset(&out_auth
),
996 * Marshall the header into the outgoing PDU.
999 if(!smb_io_rpc_hdr("", &p
->hdr
, &outgoing_rpc
, 0)) {
1000 DEBUG(0,("pi_pipe_bind_req: marshalling of RPC_HDR failed.\n"));
1005 * Now add the RPC_HDR_BA and any auth needed.
1008 if(!prs_append_prs_data( &outgoing_rpc
, &out_hdr_ba
)) {
1009 DEBUG(0,("api_pipe_bind_req: append of RPC_HDR_BA failed.\n"));
1013 if(p
->ntlmssp_auth_requested
&& !prs_append_prs_data( &outgoing_rpc
, &out_auth
)) {
1014 DEBUG(0,("api_pipe_bind_req: append of auth info failed.\n"));
1018 if(!p
->ntlmssp_auth_requested
)
1019 p
->pipe_bound
= True
;
1022 * Setup the lengths for the initial reply.
1025 p
->out_data
.data_sent_length
= 0;
1026 p
->out_data
.current_pdu_len
= prs_offset(&outgoing_rpc
);
1027 p
->out_data
.current_pdu_sent
= 0;
1029 prs_mem_free(&out_hdr_ba
);
1030 prs_mem_free(&out_auth
);
1036 prs_mem_free(&outgoing_rpc
);
1037 prs_mem_free(&out_hdr_ba
);
1038 prs_mem_free(&out_auth
);
1042 /****************************************************************************
1043 Deal with sign & seal processing on an RPC request.
1044 ****************************************************************************/
1046 BOOL
api_pipe_auth_process(pipes_struct
*p
, prs_struct
*rpc_in
)
1049 * We always negotiate the following two bits....
1051 BOOL auth_verify
= ((p
->ntlmssp_chal_flags
& NTLMSSP_NEGOTIATE_SIGN
) != 0);
1052 BOOL auth_seal
= ((p
->ntlmssp_chal_flags
& NTLMSSP_NEGOTIATE_SEAL
) != 0);
1058 auth_len
= p
->hdr
.auth_len
;
1060 if ((auth_len
!= RPC_AUTH_NTLMSSP_CHK_LEN
) && auth_verify
) {
1061 DEBUG(0,("api_pipe_auth_process: Incorrect auth_len %d.\n", auth_len
));
1066 * The following is that length of the data we must verify or unseal.
1067 * This doesn't include the RPC headers or the auth_len or the RPC_HDR_AUTH_LEN
1068 * preceeding the auth_data.
1071 data_len
= p
->hdr
.frag_len
- RPC_HEADER_LEN
- RPC_HDR_REQ_LEN
-
1072 (auth_verify
? RPC_HDR_AUTH_LEN
: 0) - auth_len
;
1074 DEBUG(5,("api_pipe_auth_process: sign: %s seal: %s data %d auth %d\n",
1075 BOOLSTR(auth_verify
), BOOLSTR(auth_seal
), data_len
, auth_len
));
1079 * The data in rpc_in doesn't contain the RPC_HEADER as this
1080 * has already been consumed.
1082 char *data
= prs_data_p(rpc_in
) + RPC_HDR_REQ_LEN
;
1083 NTLMSSPcalc_p(p
, (uchar
*)data
, data_len
);
1084 crc32
= crc32_calc_buffer(data
, data_len
);
1087 old_offset
= prs_offset(rpc_in
);
1089 if (auth_seal
|| auth_verify
) {
1090 RPC_HDR_AUTH auth_info
;
1092 if(!prs_set_offset(rpc_in
, old_offset
+ data_len
)) {
1093 DEBUG(0,("api_pipe_auth_process: cannot move offset to %u.\n",
1094 (unsigned int)old_offset
+ data_len
));
1098 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info
, rpc_in
, 0)) {
1099 DEBUG(0,("api_pipe_auth_process: failed to unmarshall RPC_HDR_AUTH.\n"));
1105 RPC_AUTH_NTLMSSP_CHK ntlmssp_chk
;
1106 char *req_data
= prs_data_p(rpc_in
) + prs_offset(rpc_in
) + 4;
1108 DEBUG(5,("api_pipe_auth_process: auth %d\n", prs_offset(rpc_in
) + 4));
1111 * Ensure we have RPC_AUTH_NTLMSSP_CHK_LEN - 4 more bytes in the
1114 if(prs_mem_get(rpc_in
, RPC_AUTH_NTLMSSP_CHK_LEN
- 4) == NULL
) {
1115 DEBUG(0,("api_pipe_auth_process: missing %d bytes in buffer.\n",
1116 RPC_AUTH_NTLMSSP_CHK_LEN
- 4 ));
1120 NTLMSSPcalc_p(p
, (uchar
*)req_data
, RPC_AUTH_NTLMSSP_CHK_LEN
- 4);
1121 if(!smb_io_rpc_auth_ntlmssp_chk("auth_sign", &ntlmssp_chk
, rpc_in
, 0)) {
1122 DEBUG(0,("api_pipe_auth_process: failed to unmarshall RPC_AUTH_NTLMSSP_CHK.\n"));
1126 if (!rpc_auth_ntlmssp_chk(&ntlmssp_chk
, crc32
, p
->ntlmssp_seq_num
)) {
1127 DEBUG(0,("api_pipe_auth_process: NTLMSSP check failed.\n"));
1133 * Return the current pointer to the data offset.
1136 if(!prs_set_offset(rpc_in
, old_offset
)) {
1137 DEBUG(0,("api_pipe_auth_process: failed to set offset back to %u\n",
1138 (unsigned int)old_offset
));
1145 /****************************************************************************
1146 Return a user struct for a pipe user.
1147 ****************************************************************************/
1149 struct current_user
*get_current_user(struct current_user
*user
, pipes_struct
*p
)
1151 if (p
->ntlmssp_auth_validated
) {
1152 memcpy(user
, &p
->pipe_user
, sizeof(struct current_user
));
1154 extern struct current_user current_user
;
1155 memcpy(user
, ¤t_user
, sizeof(struct current_user
));
1161 /****************************************************************************
1162 Find the correct RPC function to call for this request.
1163 If the pipe is authenticated then become the correct UNIX user
1164 before doing the call.
1165 ****************************************************************************/
1167 BOOL
api_pipe_request(pipes_struct
*p
)
1172 if (p
->ntlmssp_auth_validated
) {
1174 if(!become_authenticated_pipe_user(p
)) {
1175 prs_mem_free(&p
->out_data
.rdata
);
1180 for (i
= 0; api_fd_commands
[i
].pipe_clnt_name
; i
++) {
1181 if (strequal(api_fd_commands
[i
].pipe_clnt_name
, p
->name
) &&
1182 api_fd_commands
[i
].fn
!= NULL
) {
1183 DEBUG(3,("Doing \\PIPE\\%s\n", api_fd_commands
[i
].pipe_clnt_name
));
1184 set_current_rpc_talloc(p
->mem_ctx
);
1185 ret
= api_fd_commands
[i
].fn(p
);
1186 set_current_rpc_talloc(NULL
);
1190 if (p
->ntlmssp_auth_validated
)
1191 unbecome_authenticated_pipe_user();
1196 /*******************************************************************
1197 Calls the underlying RPC function for a named pipe.
1198 ********************************************************************/
1200 BOOL
api_rpcTNP(pipes_struct
*p
, const char *rpc_name
,
1201 struct api_struct
*api_rpc_cmds
)
1205 uint32 offset1
, offset2
;
1207 /* interpret the command */
1208 DEBUG(4,("api_rpcTNP: %s op 0x%x - ", rpc_name
, p
->hdr_req
.opnum
));
1210 slprintf(name
, sizeof(name
)-1, "in_%s", rpc_name
);
1211 prs_dump(name
, p
->hdr_req
.opnum
, &p
->in_data
.data
);
1213 for (fn_num
= 0; api_rpc_cmds
[fn_num
].name
; fn_num
++) {
1214 if (api_rpc_cmds
[fn_num
].opnum
== p
->hdr_req
.opnum
&& api_rpc_cmds
[fn_num
].fn
!= NULL
) {
1215 DEBUG(3,("api_rpcTNP: pipe %u rpc command: %s\n", (unsigned int)p
->pnum
, api_rpc_cmds
[fn_num
].name
));
1220 if (api_rpc_cmds
[fn_num
].name
== NULL
) {
1222 * For an unknown RPC just return a fault PDU but
1223 * return True to allow RPC's on the pipe to continue
1224 * and not put the pipe into fault state. JRA.
1226 DEBUG(4, ("unknown\n"));
1227 setup_fault_pdu(p
, NT_STATUS(0x1c010002));
1231 offset1
= prs_offset(&p
->out_data
.rdata
);
1233 /* do the actual command */
1234 if(!api_rpc_cmds
[fn_num
].fn(p
)) {
1235 DEBUG(0,("api_rpcTNP: %s: %s failed.\n", rpc_name
, api_rpc_cmds
[fn_num
].name
));
1236 prs_mem_free(&p
->out_data
.rdata
);
1240 if (p
->bad_handle_fault_state
) {
1241 DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
1242 p
->bad_handle_fault_state
= False
;
1243 setup_fault_pdu(p
, NT_STATUS(0x1C00001A));
1247 slprintf(name
, sizeof(name
)-1, "out_%s", rpc_name
);
1248 offset2
= prs_offset(&p
->out_data
.rdata
);
1249 prs_set_offset(&p
->out_data
.rdata
, offset1
);
1250 prs_dump(name
, p
->hdr_req
.opnum
, &p
->out_data
.rdata
);
1251 prs_set_offset(&p
->out_data
.rdata
, offset2
);
1253 DEBUG(5,("api_rpcTNP: called %s successfully\n", rpc_name
));
1255 /* Check for buffer underflow in rpc parsing */
1257 if ((DEBUGLEVEL
>= 10) &&
1258 (p
->in_data
.data
.data_offset
!= p
->in_data
.data
.buffer_size
)) {
1259 int data_len
= p
->in_data
.data
.buffer_size
-
1260 p
->in_data
.data
.data_offset
;
1263 data
= malloc(data_len
);
1265 DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
1267 prs_uint8s(False
, "", &p
->in_data
.data
, 0, (unsigned char *)data
,