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;
34 extern pstring global_myname;
35 extern fstring global_myworkgroup;
37 /****************************************************************************
38 Generate the next creds to use.
39 ****************************************************************************/
41 static void gen_next_creds( struct cli_state *cli, DOM_CRED *new_clnt_cred)
44 * Create the new client credentials.
47 cli->clnt_cred.timestamp.time = time(NULL);
49 memcpy(new_clnt_cred, &cli->clnt_cred, sizeof(*new_clnt_cred));
51 /* Calculate the new credentials. */
52 cred_create(cli->sess_key, &(cli->clnt_cred.challenge),
53 new_clnt_cred->timestamp, &(new_clnt_cred->challenge));
58 /****************************************************************************
59 do a LSA Logon Control2
60 ****************************************************************************/
61 BOOL cli_net_logon_ctrl2(struct cli_state *cli, uint32 status_level)
65 NET_Q_LOGON_CTRL2 q_l;
68 prs_init(&buf , 1024, 4, False);
69 prs_init(&rbuf, 0, 4, True );
71 /* create and send a MSRPC command with api NET_LOGON_CTRL2 */
73 DEBUG(4,("do_net_logon_ctrl2 from %s status level:%x\n",
74 global_myname, status_level));
76 /* store the parameters */
77 init_q_logon_ctrl2(&q_l, cli->srv_name_slash, status_level);
79 /* turn parameters into data stream */
80 if(!net_io_q_logon_ctrl2("", &q_l, &buf, 0)) {
81 DEBUG(0,("cli_net_logon_ctrl2: Error : failed to marshall NET_Q_LOGON_CTRL2 struct.\n"));
87 /* send the data on \PIPE\ */
88 if (rpc_api_pipe_req(cli, NET_LOGON_CTRL2, &buf, &rbuf))
90 NET_R_LOGON_CTRL2 r_l;
93 * Unmarshall the return buffer.
95 ok = net_io_r_logon_ctrl2("", &r_l, &rbuf, 0);
97 if (ok && r_l.status != 0)
99 /* report error code */
100 DEBUG(0,("do_net_logon_ctrl2: Error %s\n", get_nt_error_msg(r_l.status)));
101 cli->nt_error = r_l.status;
113 /****************************************************************************
116 Send the client credential, receive back a server credential.
117 Ensure that the server credential returned matches the session key
118 encrypt of the server challenge originally received. JRA.
119 ****************************************************************************/
121 BOOL cli_net_auth2(struct cli_state *cli, uint16 sec_chan,
122 uint32 neg_flags, DOM_CHAL *srv_chal)
129 prs_init(&buf , 1024, 4, False);
130 prs_init(&rbuf, 0, 4, True );
132 /* create and send a MSRPC command with api NET_AUTH2 */
134 DEBUG(4,("cli_net_auth2: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
135 cli->srv_name_slash, cli->mach_acct, sec_chan, global_myname,
136 credstr(cli->clnt_cred.challenge.data), neg_flags));
138 /* store the parameters */
139 init_q_auth_2(&q_a, cli->srv_name_slash, cli->mach_acct, sec_chan, global_myname,
140 &cli->clnt_cred.challenge, neg_flags);
142 /* turn parameters into data stream */
143 if(!net_io_q_auth_2("", &q_a, &buf, 0)) {
144 DEBUG(0,("cli_net_auth2: Error : failed to marshall NET_Q_AUTH_2 struct.\n"));
150 /* send the data on \PIPE\ */
151 if (rpc_api_pipe_req(cli, NET_AUTH2, &buf, &rbuf))
155 ok = net_io_r_auth_2("", &r_a, &rbuf, 0);
157 if (ok && r_a.status != 0)
159 /* report error code */
160 DEBUG(0,("cli_net_auth2: Error %s\n", get_nt_error_msg(r_a.status)));
161 cli->nt_error = r_a.status;
168 * Check the returned value using the initial
169 * server received challenge.
174 if(cred_assert( &r_a.srv_chal, cli->sess_key, srv_chal, zerotime) == 0) {
176 * Server replied with bad credential. Fail.
178 DEBUG(0,("cli_net_auth2: server %s replied with bad credential (bad machine \
179 password ?).\n", cli->desthost ));
186 * Try commenting this out to see if this makes the connect
187 * work for a NT 3.51 PDC. JRA.
190 if (ok && r_a.srv_flgs.neg_flags != q_a.clnt_flgs.neg_flags)
192 /* report different neg_flags */
193 DEBUG(0,("cli_net_auth2: error neg_flags (q,r) differ - (%x,%x)\n",
194 q_a.clnt_flgs.neg_flags, r_a.srv_flgs.neg_flags));
207 /****************************************************************************
208 LSA Request Challenge. Sends our challenge to server, then gets
209 server response. These are used to generate the credentials.
210 ****************************************************************************/
212 BOOL cli_net_req_chal(struct cli_state *cli, DOM_CHAL *clnt_chal, DOM_CHAL *srv_chal)
217 BOOL valid_chal = False;
219 prs_init(&buf , 1024, 4, False);
220 prs_init(&rbuf, 0, 4, True );
222 /* create and send a MSRPC command with api NET_REQCHAL */
224 DEBUG(4,("cli_net_req_chal: LSA Request Challenge from %s to %s: %s\n",
225 cli->desthost, global_myname, credstr(clnt_chal->data)));
227 /* store the parameters */
228 init_q_req_chal(&q_c, cli->srv_name_slash, global_myname, clnt_chal);
230 /* turn parameters into data stream */
231 if(!net_io_q_req_chal("", &q_c, &buf, 0)) {
232 DEBUG(0,("cli_net_req_chal: Error : failed to marshall NET_Q_REQ_CHAL struct.\n"));
238 /* send the data on \PIPE\ */
239 if (rpc_api_pipe_req(cli, NET_REQCHAL, &buf, &rbuf))
244 ok = net_io_r_req_chal("", &r_c, &rbuf, 0);
246 if (ok && r_c.status != 0)
248 /* report error code */
249 DEBUG(0,("cli_net_req_chal: Error %s\n", get_nt_error_msg(r_c.status)));
250 cli->nt_error = r_c.status;
256 /* ok, at last: we're happy. return the challenge */
257 memcpy(srv_chal, r_c.srv_chal.data, sizeof(srv_chal->data));
268 /***************************************************************************
269 LSA Server Password Set.
270 ****************************************************************************/
272 BOOL cli_net_srv_pwset(struct cli_state *cli, uint8 hashed_mach_pwd[16])
276 DOM_CRED new_clnt_cred;
279 uint16 sec_chan_type = 2;
281 gen_next_creds( cli, &new_clnt_cred);
283 prs_init(&buf , 1024, 4, False);
284 prs_init(&rbuf, 0, 4, True );
286 /* create and send a MSRPC command with api NET_SRV_PWSET */
288 DEBUG(4,("cli_net_srv_pwset: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
289 cli->srv_name_slash, cli->mach_acct, sec_chan_type, global_myname,
290 credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time));
292 /* store the parameters */
293 init_q_srv_pwset(&q_s, cli->srv_name_slash, cli->mach_acct, sec_chan_type,
294 global_myname, &new_clnt_cred, (char *)hashed_mach_pwd);
296 /* turn parameters into data stream */
297 if(!net_io_q_srv_pwset("", &q_s, &buf, 0)) {
298 DEBUG(0,("cli_net_srv_pwset: Error : failed to marshall NET_Q_SRV_PWSET struct.\n"));
304 /* send the data on \PIPE\ */
305 if (rpc_api_pipe_req(cli, NET_SRVPWSET, &buf, &rbuf))
309 ok = net_io_r_srv_pwset("", &r_s, &rbuf, 0);
311 if (ok && r_s.status != 0)
313 /* report error code */
314 DEBUG(0,("cli_net_srv_pwset: %s\n", get_nt_error_msg(r_s.status)));
315 cli->nt_error = r_s.status;
319 /* Update the credentials. */
320 if (ok && !clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_cred)))
323 * Server replied with bad credential. Fail.
325 DEBUG(0,("cli_net_srv_pwset: server %s replied with bad credential (bad machine \
326 password ?).\n", cli->desthost ));
337 /***************************************************************************
338 LSA SAM Logon - interactive or network.
339 ****************************************************************************/
341 BOOL cli_net_sam_logon(struct cli_state *cli, NET_ID_INFO_CTR *ctr,
342 NET_USER_INFO_3 *user_info3)
344 DOM_CRED new_clnt_cred;
345 DOM_CRED dummy_rtn_creds;
348 uint16 validation_level = 3;
352 gen_next_creds( cli, &new_clnt_cred);
354 prs_init(&buf , 1024, 4, False);
355 prs_init(&rbuf, 0, 4, True );
357 /* create and send a MSRPC command with api NET_SAMLOGON */
359 DEBUG(4,("cli_net_sam_logon: srv:%s mc:%s clnt %s %x ll: %d\n",
360 cli->srv_name_slash, global_myname,
361 credstr(new_clnt_cred.challenge.data), cli->clnt_cred.timestamp.time,
364 memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
365 dummy_rtn_creds.timestamp.time = time(NULL);
367 /* store the parameters */
368 q_s.validation_level = validation_level;
369 init_sam_info(&q_s.sam_id, cli->srv_name_slash, global_myname,
370 &new_clnt_cred, &dummy_rtn_creds, ctr->switch_value, ctr);
372 /* turn parameters into data stream */
373 if(!net_io_q_sam_logon("", &q_s, &buf, 0)) {
374 DEBUG(0,("cli_net_sam_logon: Error : failed to marshall NET_Q_SAM_LOGON struct.\n"));
380 /* send the data on \PIPE\ */
381 if (rpc_api_pipe_req(cli, NET_SAMLOGON, &buf, &rbuf))
385 r_s.user = user_info3;
387 ok = net_io_r_sam_logon("", &r_s, &rbuf, 0);
389 if (ok && r_s.status != 0)
391 /* report error code */
392 DEBUG(0,("cli_net_sam_logon: %s\n", get_nt_error_msg(r_s.status)));
393 cli->nt_error = r_s.status;
397 /* Update the credentials. */
398 if (ok && !clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_creds)))
401 * Server replied with bad credential. Fail.
403 DEBUG(0,("cli_net_sam_logon: server %s replied with bad credential (bad machine \
404 password ?).\n", cli->desthost ));
408 if (ok && r_s.switch_value != 3)
410 /* report different switch_value */
411 DEBUG(0,("cli_net_sam_logon: switch_value of 3 expected %x\n",
423 /***************************************************************************
426 This currently doesnt work correctly as the domain controller
427 returns NT_STATUS_INVALID_INFO_CLASS - we obviously need to
428 send a different info level. Right now though, I'm not sure
429 what that needs to be (I need to see one on the wire before
431 ****************************************************************************/
432 BOOL cli_net_sam_logoff(struct cli_state *cli, NET_ID_INFO_CTR *ctr)
434 DOM_CRED new_clnt_cred;
435 DOM_CRED dummy_rtn_creds;
438 NET_Q_SAM_LOGOFF q_s;
441 gen_next_creds( cli, &new_clnt_cred);
443 prs_init(&buf , 1024, 4, False);
444 prs_init(&rbuf, 0, 4, True );
446 /* create and send a MSRPC command with api NET_SAMLOGOFF */
448 DEBUG(4,("cli_net_sam_logoff: srv:%s mc:%s clnt %s %x ll: %d\n",
449 cli->srv_name_slash, global_myname,
450 credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time,
453 memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
455 init_sam_info(&q_s.sam_id, cli->srv_name_slash, global_myname,
456 &new_clnt_cred, &dummy_rtn_creds, ctr->switch_value, ctr);
458 /* turn parameters into data stream */
459 if(!net_io_q_sam_logoff("", &q_s, &buf, 0)) {
460 DEBUG(0,("cli_net_sam_logoff: Error : failed to marshall NET_Q_SAM_LOGOFF struct.\n"));
466 /* send the data on \PIPE\ */
467 if (rpc_api_pipe_req(cli, NET_SAMLOGOFF, &buf, &rbuf))
469 NET_R_SAM_LOGOFF r_s;
471 ok = net_io_r_sam_logoff("", &r_s, &rbuf, 0);
473 if (ok && r_s.status != 0)
475 /* report error code */
476 DEBUG(0,("cli_net_sam_logoff: %s\n", get_nt_error_msg(r_s.status)));
477 cli->nt_error = r_s.status;
481 /* Update the credentials. */
482 if (ok && !clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_creds)))
485 * Server replied with bad credential. Fail.
487 DEBUG(0,("cli_net_sam_logoff: server %s replied with bad credential (bad machine \
488 password ?).\n", cli->desthost ));
499 /*********************************************************
500 Change the domain password on the PDC.
501 **********************************************************/
503 static BOOL modify_trust_password( char *domain, char *remote_machine,
504 unsigned char orig_trust_passwd_hash[16],
505 unsigned char new_trust_passwd_hash[16])
507 struct cli_state cli;
510 if(cli_initialise(&cli) == False) {
511 DEBUG(0,("modify_trust_password: unable to initialize client connection.\n"));
515 if(!resolve_name( remote_machine, &cli.dest_ip, 0x20)) {
516 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));
526 if (!cli_connect(&cli, remote_machine, &cli.dest_ip)) {
527 DEBUG(0,("modify_trust_password: unable to connect to SMB server on \
528 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
532 if (!attempt_netbios_session_request(&cli, global_myname, remote_machine, &cli.dest_ip)) {
533 DEBUG(0,("modify_trust_password: machine %s rejected the NetBIOS \
534 session request. Error was %s\n", remote_machine, cli_errstr(&cli) ));
538 cli.protocol = PROTOCOL_NT1;
540 if (!cli_negprot(&cli)) {
541 DEBUG(0,("modify_trust_password: machine %s rejected the negotiate protocol. \
542 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
547 if (cli.protocol != PROTOCOL_NT1) {
548 DEBUG(0,("modify_trust_password: machine %s didn't negotiate NT protocol.\n",
555 * Do an anonymous session setup.
558 if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
559 DEBUG(0,("modify_trust_password: machine %s rejected the session setup. \
560 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
565 if (!(cli.sec_mode & 1)) {
566 DEBUG(0,("modify_trust_password: machine %s isn't in user level security mode\n",
572 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
573 DEBUG(0,("modify_trust_password: machine %s rejected the tconX on the IPC$ share. \
574 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
580 * Ok - we have an anonymous connection to the IPC$ share.
581 * Now start the NT Domain stuff :-).
584 if(cli_nt_session_open(&cli, PIPE_NETLOGON) == False) {
585 DEBUG(0,("modify_trust_password: unable to open the domain client session to \
586 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli)));
587 cli_nt_session_close(&cli);
593 if(cli_nt_setup_creds(&cli, orig_trust_passwd_hash) == False) {
594 DEBUG(0,("modify_trust_password: unable to setup the PDC credentials to machine \
595 %s. Error was : %s.\n", remote_machine, cli_errstr(&cli)));
596 cli_nt_session_close(&cli);
602 if( cli_nt_srv_pwset( &cli,new_trust_passwd_hash ) == False) {
603 DEBUG(0,("modify_trust_password: unable to change password for machine %s in domain \
604 %s to Domain controller %s. Error was %s.\n", global_myname, domain, remote_machine,
606 cli_close(&cli, cli.nt_pipe_fnum);
612 cli_nt_session_close(&cli);
619 /************************************************************************
620 Change the trust account password for a domain.
621 The user of this function must have locked the trust password file for
623 ************************************************************************/
625 BOOL change_trust_account_password( char *domain, char *remote_machine_list)
627 fstring remote_machine;
628 unsigned char old_trust_passwd_hash[16];
629 unsigned char new_trust_passwd_hash[16];
633 if(!get_trust_account_password( old_trust_passwd_hash, &lct)) {
634 DEBUG(0,("change_trust_account_password: unable to read the machine \
635 account password for domain %s.\n", domain));
640 * Create the new (random) password.
642 generate_random_buffer( new_trust_passwd_hash, 16, True);
644 while(remote_machine_list &&
645 next_token(&remote_machine_list, remote_machine,
646 LIST_SEP, sizeof(remote_machine))) {
647 strupper(remote_machine);
648 if(strequal(remote_machine, "*")) {
651 * We have been asked to dynamcially determine the IP addresses of the PDC.
654 struct in_addr *ip_list = NULL;
658 if(!get_dc_list(domain, &ip_list, &count))
662 * Try and connect to the PDC/BDC list in turn as an IP
663 * address used as a string.
666 for(i = 0; i < count; i++) {
668 if(!lookup_pdc_name(global_myname, domain, &ip_list[i], dc_name))
670 if((res = modify_trust_password( domain, dc_name,
671 old_trust_passwd_hash, new_trust_passwd_hash)))
676 free((char *)ip_list);
679 res = modify_trust_password( domain, remote_machine,
680 old_trust_passwd_hash, new_trust_passwd_hash);
684 DEBUG(0,("%s : change_trust_account_password: Changed password for \
685 domain %s.\n", timestring(False), domain));
687 * Return the result of trying to write the new password
688 * back into the trust account file.
690 res = set_trust_account_password(new_trust_passwd_hash);
691 memset(new_trust_passwd_hash, 0, 16);
692 memset(old_trust_passwd_hash, 0, 16);
697 memset(new_trust_passwd_hash, 0, 16);
698 memset(old_trust_passwd_hash, 0, 16);
700 DEBUG(0,("%s : change_trust_account_password: Failed to change password for \
701 domain %s.\n", timestring(False), domain));