2 * Unix SMB/Netbios implementation.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-1997,
6 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
7 * Copyright (C) Paul Ashton 1997.
8 * Copyright (C) Jeremy Allison 1998.
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.
32 extern int DEBUGLEVEL
;
33 extern pstring global_myname
;
34 extern fstring global_myworkgroup
;
36 /****************************************************************************
37 Generate the next creds to use.
38 ****************************************************************************/
40 static void gen_next_creds( struct cli_state
*cli
, DOM_CRED
*new_clnt_cred
)
43 * Create the new client credentials.
46 cli
->clnt_cred
.timestamp
.time
= time(NULL
);
48 memcpy(new_clnt_cred
, &cli
->clnt_cred
, sizeof(*new_clnt_cred
));
50 /* Calculate the new credentials. */
51 cred_create(cli
->sess_key
, &(cli
->clnt_cred
.challenge
),
52 new_clnt_cred
->timestamp
, &(new_clnt_cred
->challenge
));
57 /****************************************************************************
58 do a LSA Logon Control2
59 ****************************************************************************/
60 BOOL
cli_net_logon_ctrl2(struct cli_state
*cli
, uint32 status_level
)
64 NET_Q_LOGON_CTRL2 q_l
;
67 prs_init(&buf
, 1024, 4, cli
->mem_ctx
, False
);
68 prs_init(&rbuf
, 0, 4, cli
->mem_ctx
, True
);
70 /* create and send a MSRPC command with api NET_LOGON_CTRL2 */
72 DEBUG(4,("do_net_logon_ctrl2 from %s status level:%x\n",
73 global_myname
, status_level
));
75 /* store the parameters */
76 init_q_logon_ctrl2(&q_l
, cli
->srv_name_slash
, status_level
);
78 /* turn parameters into data stream */
79 if(!net_io_q_logon_ctrl2("", &q_l
, &buf
, 0)) {
80 DEBUG(0,("cli_net_logon_ctrl2: Error : failed to marshall NET_Q_LOGON_CTRL2 struct.\n"));
86 /* send the data on \PIPE\ */
87 if (rpc_api_pipe_req(cli
, NET_LOGON_CTRL2
, &buf
, &rbuf
))
89 NET_R_LOGON_CTRL2 r_l
;
92 * Unmarshall the return buffer.
94 ok
= net_io_r_logon_ctrl2("", &r_l
, &rbuf
, 0);
96 if (ok
&& r_l
.status
!= 0)
98 /* report error code */
99 DEBUG(0,("do_net_logon_ctrl2: Error %s\n", get_nt_error_msg(r_l
.status
)));
100 cli
->nt_error
= r_l
.status
;
112 /****************************************************************************
115 Send the client credential, receive back a server credential.
116 Ensure that the server credential returned matches the session key
117 encrypt of the server challenge originally received. JRA.
118 ****************************************************************************/
120 BOOL
cli_net_auth2(struct cli_state
*cli
, uint16 sec_chan
,
121 uint32 neg_flags
, DOM_CHAL
*srv_chal
)
128 prs_init(&buf
, 1024, 4, cli
->mem_ctx
, False
);
129 prs_init(&rbuf
, 0, 4, cli
->mem_ctx
, True
);
131 /* create and send a MSRPC command with api NET_AUTH2 */
133 DEBUG(4,("cli_net_auth2: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
134 cli
->srv_name_slash
, cli
->mach_acct
, sec_chan
, global_myname
,
135 credstr(cli
->clnt_cred
.challenge
.data
), neg_flags
));
137 /* store the parameters */
138 init_q_auth_2(&q_a
, cli
->srv_name_slash
, cli
->mach_acct
, sec_chan
, global_myname
,
139 &cli
->clnt_cred
.challenge
, neg_flags
);
141 /* turn parameters into data stream */
142 if(!net_io_q_auth_2("", &q_a
, &buf
, 0)) {
143 DEBUG(0,("cli_net_auth2: Error : failed to marshall NET_Q_AUTH_2 struct.\n"));
149 /* send the data on \PIPE\ */
150 if (rpc_api_pipe_req(cli
, NET_AUTH2
, &buf
, &rbuf
))
154 ok
= net_io_r_auth_2("", &r_a
, &rbuf
, 0);
156 if (ok
&& r_a
.status
!= 0)
158 /* report error code */
159 DEBUG(0,("cli_net_auth2: Error %s\n", get_nt_error_msg(r_a
.status
)));
160 cli
->nt_error
= r_a
.status
;
167 * Check the returned value using the initial
168 * server received challenge.
173 if(cred_assert( &r_a
.srv_chal
, cli
->sess_key
, srv_chal
, zerotime
) == 0) {
175 * Server replied with bad credential. Fail.
177 DEBUG(0,("cli_net_auth2: server %s replied with bad credential (bad machine \
178 password ?).\n", cli
->desthost
));
185 * Try commenting this out to see if this makes the connect
186 * work for a NT 3.51 PDC. JRA.
189 if (ok
&& r_a
.srv_flgs
.neg_flags
!= q_a
.clnt_flgs
.neg_flags
)
191 /* report different neg_flags */
192 DEBUG(0,("cli_net_auth2: error neg_flags (q,r) differ - (%x,%x)\n",
193 q_a
.clnt_flgs
.neg_flags
, r_a
.srv_flgs
.neg_flags
));
206 /****************************************************************************
207 LSA Request Challenge. Sends our challenge to server, then gets
208 server response. These are used to generate the credentials.
209 ****************************************************************************/
211 BOOL
cli_net_req_chal(struct cli_state
*cli
, DOM_CHAL
*clnt_chal
, DOM_CHAL
*srv_chal
)
216 BOOL valid_chal
= False
;
218 prs_init(&buf
, 1024, 4, cli
->mem_ctx
, False
);
219 prs_init(&rbuf
, 0, 4, cli
->mem_ctx
, True
);
221 /* create and send a MSRPC command with api NET_REQCHAL */
223 DEBUG(4,("cli_net_req_chal: LSA Request Challenge from %s to %s: %s\n",
224 cli
->desthost
, global_myname
, credstr(clnt_chal
->data
)));
226 /* store the parameters */
227 init_q_req_chal(&q_c
, cli
->srv_name_slash
, global_myname
, clnt_chal
);
229 /* turn parameters into data stream */
230 if(!net_io_q_req_chal("", &q_c
, &buf
, 0)) {
231 DEBUG(0,("cli_net_req_chal: Error : failed to marshall NET_Q_REQ_CHAL struct.\n"));
237 /* send the data on \PIPE\ */
238 if (rpc_api_pipe_req(cli
, NET_REQCHAL
, &buf
, &rbuf
))
243 ok
= net_io_r_req_chal("", &r_c
, &rbuf
, 0);
245 if (ok
&& r_c
.status
!= 0)
247 /* report error code */
248 DEBUG(0,("cli_net_req_chal: Error %s\n", get_nt_error_msg(r_c
.status
)));
249 cli
->nt_error
= r_c
.status
;
255 /* ok, at last: we're happy. return the challenge */
256 memcpy(srv_chal
, r_c
.srv_chal
.data
, sizeof(srv_chal
->data
));
267 /***************************************************************************
268 LSA Server Password Set.
269 ****************************************************************************/
271 BOOL
cli_net_srv_pwset(struct cli_state
*cli
, uint8 hashed_mach_pwd
[16])
275 DOM_CRED new_clnt_cred
;
278 uint16 sec_chan_type
= 2;
280 gen_next_creds( cli
, &new_clnt_cred
);
282 prs_init(&buf
, 1024, 4, cli
->mem_ctx
, False
);
283 prs_init(&rbuf
, 0, 4, cli
->mem_ctx
, True
);
285 /* create and send a MSRPC command with api NET_SRV_PWSET */
287 DEBUG(4,("cli_net_srv_pwset: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
288 cli
->srv_name_slash
, cli
->mach_acct
, sec_chan_type
, global_myname
,
289 credstr(new_clnt_cred
.challenge
.data
), new_clnt_cred
.timestamp
.time
));
291 /* store the parameters */
292 init_q_srv_pwset(&q_s
, cli
->srv_name_slash
, cli
->mach_acct
, sec_chan_type
,
293 global_myname
, &new_clnt_cred
, (char *)hashed_mach_pwd
);
295 /* turn parameters into data stream */
296 if(!net_io_q_srv_pwset("", &q_s
, &buf
, 0)) {
297 DEBUG(0,("cli_net_srv_pwset: Error : failed to marshall NET_Q_SRV_PWSET struct.\n"));
303 /* send the data on \PIPE\ */
304 if (rpc_api_pipe_req(cli
, NET_SRVPWSET
, &buf
, &rbuf
))
308 ok
= net_io_r_srv_pwset("", &r_s
, &rbuf
, 0);
310 if (ok
&& r_s
.status
!= 0)
312 /* report error code */
313 DEBUG(0,("cli_net_srv_pwset: %s\n", get_nt_error_msg(r_s
.status
)));
314 cli
->nt_error
= r_s
.status
;
318 /* Update the credentials. */
319 if (ok
&& !clnt_deal_with_creds(cli
->sess_key
, &(cli
->clnt_cred
), &(r_s
.srv_cred
)))
322 * Server replied with bad credential. Fail.
324 DEBUG(0,("cli_net_srv_pwset: server %s replied with bad credential (bad machine \
325 password ?).\n", cli
->desthost
));
336 /***************************************************************************
337 LSA SAM Logon - interactive or network.
338 ****************************************************************************/
340 BOOL
cli_net_sam_logon(struct cli_state
*cli
, NET_ID_INFO_CTR
*ctr
,
341 NET_USER_INFO_3
*user_info3
)
343 DOM_CRED new_clnt_cred
;
344 DOM_CRED dummy_rtn_creds
;
347 uint16 validation_level
= 3;
351 gen_next_creds( cli
, &new_clnt_cred
);
353 prs_init(&buf
, 1024, 4, cli
->mem_ctx
, False
);
354 prs_init(&rbuf
, 0, 4, cli
->mem_ctx
, True
);
356 /* create and send a MSRPC command with api NET_SAMLOGON */
358 DEBUG(4,("cli_net_sam_logon: srv:%s mc:%s clnt %s %x ll: %d\n",
359 cli
->srv_name_slash
, global_myname
,
360 credstr(new_clnt_cred
.challenge
.data
), cli
->clnt_cred
.timestamp
.time
,
363 memset(&dummy_rtn_creds
, '\0', sizeof(dummy_rtn_creds
));
364 dummy_rtn_creds
.timestamp
.time
= time(NULL
);
366 /* store the parameters */
367 q_s
.validation_level
= validation_level
;
368 init_sam_info(&q_s
.sam_id
, cli
->srv_name_slash
, global_myname
,
369 &new_clnt_cred
, &dummy_rtn_creds
, ctr
->switch_value
, ctr
);
371 /* turn parameters into data stream */
372 if(!net_io_q_sam_logon("", &q_s
, &buf
, 0)) {
373 DEBUG(0,("cli_net_sam_logon: Error : failed to marshall NET_Q_SAM_LOGON struct.\n"));
379 /* send the data on \PIPE\ */
380 if (rpc_api_pipe_req(cli
, NET_SAMLOGON
, &buf
, &rbuf
))
384 r_s
.user
= user_info3
;
386 ok
= net_io_r_sam_logon("", &r_s
, &rbuf
, 0);
388 if (ok
&& r_s
.status
!= 0)
390 /* report error code */
391 DEBUG(0,("cli_net_sam_logon: %s\n", get_nt_error_msg(r_s
.status
)));
392 cli
->nt_error
= r_s
.status
;
396 /* Update the credentials. */
397 if (ok
&& !clnt_deal_with_creds(cli
->sess_key
, &(cli
->clnt_cred
), &(r_s
.srv_creds
)))
400 * Server replied with bad credential. Fail.
402 DEBUG(0,("cli_net_sam_logon: server %s replied with bad credential (bad machine \
403 password ?).\n", cli
->desthost
));
407 if (ok
&& r_s
.switch_value
!= 3)
409 /* report different switch_value */
410 DEBUG(0,("cli_net_sam_logon: switch_value of 3 expected %x\n",
422 /***************************************************************************
425 This currently doesnt work correctly as the domain controller
426 returns NT_STATUS_INVALID_INFO_CLASS - we obviously need to
427 send a different info level. Right now though, I'm not sure
428 what that needs to be (I need to see one on the wire before
430 ****************************************************************************/
431 BOOL
cli_net_sam_logoff(struct cli_state
*cli
, NET_ID_INFO_CTR
*ctr
)
433 DOM_CRED new_clnt_cred
;
434 DOM_CRED dummy_rtn_creds
;
437 NET_Q_SAM_LOGOFF q_s
;
440 gen_next_creds( cli
, &new_clnt_cred
);
442 prs_init(&buf
, 1024, 4, cli
->mem_ctx
, False
);
443 prs_init(&rbuf
, 0, 4, cli
->mem_ctx
, True
);
445 /* create and send a MSRPC command with api NET_SAMLOGOFF */
447 DEBUG(4,("cli_net_sam_logoff: srv:%s mc:%s clnt %s %x ll: %d\n",
448 cli
->srv_name_slash
, global_myname
,
449 credstr(new_clnt_cred
.challenge
.data
), new_clnt_cred
.timestamp
.time
,
452 memset(&dummy_rtn_creds
, '\0', sizeof(dummy_rtn_creds
));
454 init_sam_info(&q_s
.sam_id
, cli
->srv_name_slash
, global_myname
,
455 &new_clnt_cred
, &dummy_rtn_creds
, ctr
->switch_value
, ctr
);
457 /* turn parameters into data stream */
458 if(!net_io_q_sam_logoff("", &q_s
, &buf
, 0)) {
459 DEBUG(0,("cli_net_sam_logoff: Error : failed to marshall NET_Q_SAM_LOGOFF struct.\n"));
465 /* send the data on \PIPE\ */
466 if (rpc_api_pipe_req(cli
, NET_SAMLOGOFF
, &buf
, &rbuf
))
468 NET_R_SAM_LOGOFF r_s
;
470 ok
= net_io_r_sam_logoff("", &r_s
, &rbuf
, 0);
472 if (ok
&& r_s
.status
!= 0)
474 /* report error code */
475 DEBUG(0,("cli_net_sam_logoff: %s\n", get_nt_error_msg(r_s
.status
)));
476 cli
->nt_error
= r_s
.status
;
480 /* Update the credentials. */
481 if (ok
&& !clnt_deal_with_creds(cli
->sess_key
, &(cli
->clnt_cred
), &(r_s
.srv_creds
)))
484 * Server replied with bad credential. Fail.
486 DEBUG(0,("cli_net_sam_logoff: server %s replied with bad credential (bad machine \
487 password ?).\n", cli
->desthost
));
498 /*********************************************************
499 Change the domain password on the PDC.
500 **********************************************************/
502 static BOOL
modify_trust_password( char *domain
, char *remote_machine
,
503 unsigned char orig_trust_passwd_hash
[16],
504 unsigned char new_trust_passwd_hash
[16])
506 struct cli_state cli
;
509 if(cli_initialise(&cli
) == False
) {
510 DEBUG(0,("modify_trust_password: unable to initialize client connection.\n"));
514 if(!resolve_name( remote_machine
, &cli
.dest_ip
, 0x20)) {
515 DEBUG(0,("modify_trust_password: Can't resolve address for %s\n", remote_machine
));
520 if (ismyip(cli
.dest_ip
)) {
521 DEBUG(0,("modify_trust_password: Machine %s is one of our addresses. Cannot add \
522 to ourselves.\n", remote_machine
));
527 if (!cli_connect(&cli
, remote_machine
, &cli
.dest_ip
)) {
528 DEBUG(0,("modify_trust_password: unable to connect to SMB server on \
529 machine %s. Error was : %s.\n", remote_machine
, cli_errstr(&cli
) ));
534 if (!attempt_netbios_session_request(&cli
, global_myname
, remote_machine
, &cli
.dest_ip
)) {
535 DEBUG(0,("modify_trust_password: machine %s rejected the NetBIOS \
536 session request. Error was %s\n", remote_machine
, cli_errstr(&cli
) ));
541 cli
.protocol
= PROTOCOL_NT1
;
543 if (!cli_negprot(&cli
)) {
544 DEBUG(0,("modify_trust_password: machine %s rejected the negotiate protocol. \
545 Error was : %s.\n", remote_machine
, cli_errstr(&cli
) ));
550 if (cli
.protocol
!= PROTOCOL_NT1
) {
551 DEBUG(0,("modify_trust_password: machine %s didn't negotiate NT protocol.\n",
558 * Do an anonymous session setup.
561 if (!cli_session_setup(&cli
, "", "", 0, "", 0, "")) {
562 DEBUG(0,("modify_trust_password: machine %s rejected the session setup. \
563 Error was : %s.\n", remote_machine
, cli_errstr(&cli
) ));
568 if (!(cli
.sec_mode
& 1)) {
569 DEBUG(0,("modify_trust_password: machine %s isn't in user level security mode\n",
575 if (!cli_send_tconX(&cli
, "IPC$", "IPC", "", 1)) {
576 DEBUG(0,("modify_trust_password: machine %s rejected the tconX on the IPC$ share. \
577 Error was : %s.\n", remote_machine
, cli_errstr(&cli
) ));
583 * Ok - we have an anonymous connection to the IPC$ share.
584 * Now start the NT Domain stuff :-).
587 if(cli_lsa_get_domain_sid(&cli
, remote_machine
) == False
) {
588 DEBUG(0,("modify_trust_password: unable to obtain domain sid from %s. Error was : %s.\n", remote_machine
, cli_errstr(&cli
)));
594 if(cli_nt_session_open(&cli
, PIPE_NETLOGON
) == False
) {
595 DEBUG(0,("modify_trust_password: unable to open the domain client session to \
596 machine %s. Error was : %s.\n", remote_machine
, cli_errstr(&cli
)));
597 cli_nt_session_close(&cli
);
603 if(cli_nt_setup_creds(&cli
, orig_trust_passwd_hash
) == False
) {
604 DEBUG(0,("modify_trust_password: unable to setup the PDC credentials to machine \
605 %s. Error was : %s.\n", remote_machine
, cli_errstr(&cli
)));
606 cli_nt_session_close(&cli
);
612 if( cli_nt_srv_pwset( &cli
,new_trust_passwd_hash
) == False
) {
613 DEBUG(0,("modify_trust_password: unable to change password for machine %s in domain \
614 %s to Domain controller %s. Error was %s.\n", global_myname
, domain
, remote_machine
,
616 cli_close(&cli
, cli
.nt_pipe_fnum
);
622 cli_nt_session_close(&cli
);
629 /************************************************************************
630 Change the trust account password for a domain.
631 The user of this function must have locked the trust password file for
633 ************************************************************************/
635 BOOL
change_trust_account_password( char *domain
, char *remote_machine_list
)
637 fstring remote_machine
;
638 unsigned char old_trust_passwd_hash
[16];
639 unsigned char new_trust_passwd_hash
[16];
643 if(!secrets_fetch_trust_account_password(domain
, old_trust_passwd_hash
, &lct
)) {
644 DEBUG(0,("change_trust_account_password: unable to read the machine \
645 account password for domain %s.\n", domain
));
650 * Create the new (random) password.
652 generate_random_buffer( new_trust_passwd_hash
, 16, True
);
654 while(remote_machine_list
&&
655 next_token(&remote_machine_list
, remote_machine
,
656 LIST_SEP
, sizeof(remote_machine
))) {
657 strupper(remote_machine
);
658 if(strequal(remote_machine
, "*")) {
661 * We have been asked to dynamcially determine the IP addresses of the PDC.
664 struct in_addr
*ip_list
= NULL
;
668 if(!get_dc_list(domain
, &ip_list
, &count
))
672 * Try and connect to the PDC/BDC list in turn as an IP
673 * address used as a string.
676 for(i
= 0; i
< count
; i
++) {
678 if(!lookup_pdc_name(global_myname
, domain
, &ip_list
[i
], dc_name
))
680 if((res
= modify_trust_password( domain
, dc_name
,
681 old_trust_passwd_hash
, new_trust_passwd_hash
)))
686 free((char *)ip_list
);
689 res
= modify_trust_password( domain
, remote_machine
,
690 old_trust_passwd_hash
, new_trust_passwd_hash
);
694 DEBUG(0,("%s : change_trust_account_password: Changed password for \
695 domain %s.\n", timestring(False
), domain
));
697 * Return the result of trying to write the new password
698 * back into the trust account file.
700 res
= secrets_store_trust_account_password(domain
, new_trust_passwd_hash
);
701 memset(new_trust_passwd_hash
, 0, 16);
702 memset(old_trust_passwd_hash
, 0, 16);
707 memset(new_trust_passwd_hash
, 0, 16);
708 memset(old_trust_passwd_hash
, 0, 16);
710 DEBUG(0,("%s : change_trust_account_password: Failed to change password for \
711 domain %s.\n", timestring(False
), domain
));