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, SAFETY_MARGIN, False);
69 prs_init(&rbuf, 0, 4, SAFETY_MARGIN, 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 make_q_logon_ctrl2(&q_l, cli->srv_name_slash, status_level);
79 /* turn parameters into data stream */
80 net_io_q_logon_ctrl2("", &q_l, &buf, 0);
82 /* send the data on \PIPE\ */
83 if (rpc_api_pipe_req(cli, NET_LOGON_CTRL2, &buf, &rbuf))
85 NET_R_LOGON_CTRL2 r_l;
87 net_io_r_logon_ctrl2("", &r_l, &rbuf, 0);
88 ok = (rbuf.offset != 0);
90 if (ok && r_l.status != 0)
92 /* report error code */
93 DEBUG(0,("do_net_logon_ctrl2: Error %s\n", get_nt_error_msg(r_l.status)));
94 cli->nt_error = r_l.status;
106 /****************************************************************************
109 Send the client credential, receive back a server credential.
110 Ensure that the server credential returned matches the session key
111 encrypt of the server challenge originally received. JRA.
112 ****************************************************************************/
114 BOOL cli_net_auth2(struct cli_state *cli, uint16 sec_chan,
115 uint32 neg_flags, DOM_CHAL *srv_chal)
122 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
123 prs_init(&rbuf, 0, 4, SAFETY_MARGIN, True );
125 /* create and send a MSRPC command with api NET_AUTH2 */
127 DEBUG(4,("cli_net_auth2: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
128 cli->srv_name_slash, cli->mach_acct, sec_chan, global_myname,
129 credstr(cli->clnt_cred.challenge.data), neg_flags));
131 /* store the parameters */
132 make_q_auth_2(&q_a, cli->srv_name_slash, cli->mach_acct, sec_chan, global_myname,
133 &cli->clnt_cred.challenge, neg_flags);
135 /* turn parameters into data stream */
136 net_io_q_auth_2("", &q_a, &buf, 0);
138 /* send the data on \PIPE\ */
139 if (rpc_api_pipe_req(cli, NET_AUTH2, &buf, &rbuf))
143 net_io_r_auth_2("", &r_a, &rbuf, 0);
144 ok = (rbuf.offset != 0);
146 if (ok && r_a.status != 0)
148 /* report error code */
149 DEBUG(0,("cli_net_auth2: Error %s\n", get_nt_error_msg(r_a.status)));
150 cli->nt_error = r_a.status;
157 * Check the returned value using the initial
158 * server received challenge.
163 if(cred_assert( &r_a.srv_chal, cli->sess_key, srv_chal, zerotime) == 0) {
165 * Server replied with bad credential. Fail.
167 DEBUG(0,("cli_net_auth2: server %s replied with bad credential (bad machine \
168 password ?).\n", cli->desthost ));
173 if (ok && r_a.srv_flgs.neg_flags != q_a.clnt_flgs.neg_flags)
175 /* report different neg_flags */
176 DEBUG(0,("cli_net_auth2: error neg_flags (q,r) differ - (%x,%x)\n",
177 q_a.clnt_flgs.neg_flags, r_a.srv_flgs.neg_flags));
189 /****************************************************************************
190 LSA Request Challenge. Sends our challenge to server, then gets
191 server response. These are used to generate the credentials.
192 ****************************************************************************/
194 BOOL cli_net_req_chal(struct cli_state *cli, DOM_CHAL *clnt_chal, DOM_CHAL *srv_chal)
199 BOOL valid_chal = False;
201 if (srv_chal == NULL || clnt_chal == NULL)
204 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
205 prs_init(&rbuf, 0, 4, SAFETY_MARGIN, True );
207 /* create and send a MSRPC command with api NET_REQCHAL */
209 DEBUG(4,("cli_net_req_chal: LSA Request Challenge from %s to %s: %s\n",
210 cli->desthost, global_myname, credstr(clnt_chal->data)));
212 /* store the parameters */
213 make_q_req_chal(&q_c, cli->srv_name_slash, global_myname, clnt_chal);
215 /* turn parameters into data stream */
216 net_io_q_req_chal("", &q_c, &buf, 0);
218 /* send the data on \PIPE\ */
219 if (rpc_api_pipe_req(cli, NET_REQCHAL, &buf, &rbuf))
224 net_io_r_req_chal("", &r_c, &rbuf, 0);
225 ok = (rbuf.offset != 0);
227 if (ok && r_c.status != 0)
229 /* report error code */
230 DEBUG(0,("cli_net_req_chal: Error %s\n", get_nt_error_msg(r_c.status)));
231 cli->nt_error = r_c.status;
237 /* ok, at last: we're happy. return the challenge */
238 memcpy(srv_chal, r_c.srv_chal.data, sizeof(srv_chal->data));
249 /***************************************************************************
250 LSA Server Password Set.
251 ****************************************************************************/
253 BOOL cli_net_srv_pwset(struct cli_state *cli, uint8 hashed_mach_pwd[16])
257 DOM_CRED new_clnt_cred;
260 uint16 sec_chan_type = 2;
262 gen_next_creds( cli, &new_clnt_cred);
264 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
265 prs_init(&rbuf, 0, 4, SAFETY_MARGIN, True );
267 /* create and send a MSRPC command with api NET_SRV_PWSET */
269 DEBUG(4,("cli_net_srv_pwset: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
270 cli->srv_name_slash, cli->mach_acct, sec_chan_type, global_myname,
271 credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time));
273 /* store the parameters */
274 make_q_srv_pwset(&q_s, cli->srv_name_slash, cli->mach_acct, sec_chan_type,
275 global_myname, &new_clnt_cred, (char *)hashed_mach_pwd);
277 /* turn parameters into data stream */
278 net_io_q_srv_pwset("", &q_s, &buf, 0);
280 /* send the data on \PIPE\ */
281 if (rpc_api_pipe_req(cli, NET_SRVPWSET, &buf, &rbuf))
285 net_io_r_srv_pwset("", &r_s, &rbuf, 0);
286 ok = (rbuf.offset != 0);
288 if (ok && r_s.status != 0)
290 /* report error code */
291 DEBUG(0,("cli_net_srv_pwset: %s\n", get_nt_error_msg(r_s.status)));
292 cli->nt_error = r_s.status;
296 /* Update the credentials. */
297 if (ok && !clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_cred)))
300 * Server replied with bad credential. Fail.
302 DEBUG(0,("cli_net_srv_pwset: server %s replied with bad credential (bad machine \
303 password ?).\n", cli->desthost ));
314 /***************************************************************************
315 LSA SAM Logon - interactive or network.
316 ****************************************************************************/
318 BOOL cli_net_sam_logon(struct cli_state *cli, NET_ID_INFO_CTR *ctr,
319 NET_USER_INFO_3 *user_info3)
321 DOM_CRED new_clnt_cred;
322 DOM_CRED dummy_rtn_creds;
325 uint16 validation_level = 3;
329 gen_next_creds( cli, &new_clnt_cred);
331 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
332 prs_init(&rbuf, 0, 4, SAFETY_MARGIN, True );
334 /* create and send a MSRPC command with api NET_SAMLOGON */
336 DEBUG(4,("cli_net_sam_logon: srv:%s mc:%s clnt %s %x ll: %d\n",
337 cli->srv_name_slash, global_myname,
338 credstr(new_clnt_cred.challenge.data), cli->clnt_cred.timestamp.time,
341 memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
342 dummy_rtn_creds.timestamp.time = time(NULL);
344 /* store the parameters */
345 make_sam_info(&(q_s.sam_id), cli->srv_name_slash, global_myname,
346 &new_clnt_cred, &dummy_rtn_creds, ctr->switch_value, ctr, validation_level);
348 /* turn parameters into data stream */
349 net_io_q_sam_logon("", &q_s, &buf, 0);
351 /* send the data on \PIPE\ */
352 if (rpc_api_pipe_req(cli, NET_SAMLOGON, &buf, &rbuf))
356 r_s.user = user_info3;
358 net_io_r_sam_logon("", &r_s, &rbuf, 0);
359 ok = (rbuf.offset != 0);
361 if (ok && r_s.status != 0)
363 /* report error code */
364 DEBUG(0,("cli_net_sam_logon: %s\n", get_nt_error_msg(r_s.status)));
365 cli->nt_error = r_s.status;
369 /* Update the credentials. */
370 if (ok && !clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_creds)))
373 * Server replied with bad credential. Fail.
375 DEBUG(0,("cli_net_sam_logon: server %s replied with bad credential (bad machine \
376 password ?).\n", cli->desthost ));
380 if (ok && r_s.switch_value != 3)
382 /* report different switch_value */
383 DEBUG(0,("cli_net_sam_logon: switch_value of 3 expected %x\n",
395 /***************************************************************************
398 This currently doesnt work correctly as the domain controller
399 returns NT_STATUS_INVALID_INFO_CLASS - we obviously need to
400 send a different info level. Right now though, I'm not sure
401 what that needs to be (I need to see one on the wire before
403 ****************************************************************************/
404 BOOL cli_net_sam_logoff(struct cli_state *cli, NET_ID_INFO_CTR *ctr)
406 DOM_CRED new_clnt_cred;
407 DOM_CRED dummy_rtn_creds;
410 NET_Q_SAM_LOGOFF q_s;
411 uint16 validation_level = 3;
414 gen_next_creds( cli, &new_clnt_cred);
416 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
417 prs_init(&rbuf, 0, 4, SAFETY_MARGIN, True );
419 /* create and send a MSRPC command with api NET_SAMLOGOFF */
421 DEBUG(4,("cli_net_sam_logoff: srv:%s mc:%s clnt %s %x ll: %d\n",
422 cli->srv_name_slash, global_myname,
423 credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time,
426 memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
428 /* store the parameters */
429 make_sam_info(&(q_s.sam_id), cli->srv_name_slash, global_myname,
430 &new_clnt_cred, &dummy_rtn_creds, ctr->switch_value, ctr, validation_level);
432 /* turn parameters into data stream */
433 net_io_q_sam_logoff("", &q_s, &buf, 0);
435 /* send the data on \PIPE\ */
436 if (rpc_api_pipe_req(cli, NET_SAMLOGOFF, &buf, &rbuf))
438 NET_R_SAM_LOGOFF r_s;
440 net_io_r_sam_logoff("", &r_s, &rbuf, 0);
441 ok = (rbuf.offset != 0);
443 if (ok && r_s.status != 0)
445 /* report error code */
446 DEBUG(0,("cli_net_sam_logoff: %s\n", get_nt_error_msg(r_s.status)));
447 cli->nt_error = r_s.status;
451 /* Update the credentials. */
452 if (ok && !clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_creds)))
455 * Server replied with bad credential. Fail.
457 DEBUG(0,("cli_net_sam_logoff: server %s replied with bad credential (bad machine \
458 password ?).\n", cli->desthost ));
469 /*********************************************************
470 Change the domain password on the PDC.
471 **********************************************************/
473 static BOOL modify_trust_password( char *domain, char *remote_machine,
474 unsigned char orig_trust_passwd_hash[16],
475 unsigned char new_trust_passwd_hash[16])
477 struct cli_state cli;
478 struct nmb_name calling, called;
481 if(cli_initialise(&cli) == False) {
482 DEBUG(0,("modify_trust_password: unable to initialize client connection.\n"));
486 if(!resolve_name( remote_machine, &cli.dest_ip, 0x20)) {
487 DEBUG(0,("modify_trust_password: Can't resolve address for %s\n", remote_machine));
491 if (ismyip(cli.dest_ip)) {
492 DEBUG(0,("modify_trust_password: Machine %s is one of our addresses. Cannot add \
493 to ourselves.\n", remote_machine));
497 if (!cli_connect(&cli, remote_machine, &cli.dest_ip)) {
498 DEBUG(0,("modify_trust_password: unable to connect to SMB server on \
499 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
504 make_nmb_name(&calling, global_myname , 0x0 , scope);
505 make_nmb_name(&called , remote_machine, 0x20, scope);
507 if (!cli_session_request(&cli, &calling, &called))
509 DEBUG(0,("modify_trust_password: machine %s rejected the session setup. \
510 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
515 cli.protocol = PROTOCOL_NT1;
517 if (!cli_negprot(&cli)) {
518 DEBUG(0,("modify_trust_password: machine %s rejected the negotiate protocol. \
519 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
523 if (cli.protocol != PROTOCOL_NT1) {
524 DEBUG(0,("modify_trust_password: machine %s didn't negotiate NT protocol.\n",
531 * Do an anonymous session setup.
534 if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
535 DEBUG(0,("modify_trust_password: machine %s rejected the session setup. \
536 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
541 if (!(cli.sec_mode & 1)) {
542 DEBUG(0,("modify_trust_password: machine %s isn't in user level security mode\n",
548 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
549 DEBUG(0,("modify_trust_password: machine %s rejected the tconX on the IPC$ share. \
550 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
556 * Ok - we have an anonymous connection to the IPC$ share.
557 * Now start the NT Domain stuff :-).
560 if(cli_nt_session_open(&cli, PIPE_NETLOGON) == False) {
561 DEBUG(0,("modify_trust_password: unable to open the domain client session to \
562 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli)));
563 cli_nt_session_close(&cli);
569 if(cli_nt_setup_creds(&cli, orig_trust_passwd_hash) == False) {
570 DEBUG(0,("modify_trust_password: unable to setup the PDC credentials to machine \
571 %s. Error was : %s.\n", remote_machine, cli_errstr(&cli)));
572 cli_nt_session_close(&cli);
578 if( cli_nt_srv_pwset( &cli,new_trust_passwd_hash ) == False) {
579 DEBUG(0,("modify_trust_password: unable to change password for machine %s in domain \
580 %s to Domain controller %s. Error was %s.\n", global_myname, domain, remote_machine,
582 cli_close(&cli, cli.nt_pipe_fnum);
588 cli_nt_session_close(&cli);
595 /************************************************************************
596 Change the trust account password for a domain.
597 The user of this function must have locked the trust password file for
599 ************************************************************************/
601 BOOL change_trust_account_password( char *domain, char *remote_machine_list)
603 fstring remote_machine;
604 unsigned char old_trust_passwd_hash[16];
605 unsigned char new_trust_passwd_hash[16];
609 if(!get_trust_account_password( old_trust_passwd_hash, &lct)) {
610 DEBUG(0,("change_trust_account_password: unable to read the machine \
611 account password for domain %s.\n", domain));
616 * Create the new (random) password.
618 generate_random_buffer( new_trust_passwd_hash, 16, True);
620 while(remote_machine_list &&
621 next_token(&remote_machine_list, remote_machine,
622 LIST_SEP, sizeof(remote_machine))) {
623 strupper(remote_machine);
624 if(modify_trust_password( domain, remote_machine,
625 old_trust_passwd_hash, new_trust_passwd_hash)) {
626 DEBUG(0,("%s : change_trust_account_password: Changed password for \
627 domain %s.\n", timestring(), domain));
629 * Return the result of trying to write the new password
630 * back into the trust account file.
632 res = set_trust_account_password(new_trust_passwd_hash);
633 memset(new_trust_passwd_hash, 0, 16);
634 memset(old_trust_passwd_hash, 0, 16);
639 memset(new_trust_passwd_hash, 0, 16);
640 memset(old_trust_passwd_hash, 0, 16);
642 DEBUG(0,("%s : change_trust_account_password: Failed to change password for \
643 domain %s.\n", timestring(), domain));