2 Unix SMB/CIFS implementation.
3 NT Domain Authentication SMB / MSRPC client
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Luke Kenneth Casson Leighton 1996-2000
6 Copyright (C) Tim Potter 2001
7 Copyright (C) Paul Ashton 1997.
8 Copyright (C) Jeremy Allison 1998.
9 Copyright (C) Andrew Bartlett 2001.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 /* LSA Request Challenge. Sends our challenge to server, then gets
29 server response. These are used to generate the credentials. */
31 NTSTATUS cli_net_req_chal(struct cli_state *cli, DOM_CHAL *clnt_chal,
34 prs_struct qbuf, rbuf;
37 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
39 prs_init(&qbuf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
40 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
42 /* create and send a MSRPC command with api NET_REQCHAL */
44 DEBUG(4,("cli_net_req_chal: LSA Request Challenge from %s to %s: %s\n",
45 global_myname(), cli->desthost, credstr(clnt_chal->data)));
47 /* store the parameters */
48 init_q_req_chal(&q, cli->srv_name_slash, global_myname(), clnt_chal);
50 /* Marshall data and send request */
52 if (!net_io_q_req_chal("", &q, &qbuf, 0) ||
53 !rpc_api_pipe_req(cli, PI_NETLOGON, NET_REQCHAL, &qbuf, &rbuf)) {
57 /* Unmarhall response */
59 if (!net_io_r_req_chal("", &r, &rbuf, 0)) {
67 if (NT_STATUS_IS_OK(result)) {
68 memcpy(srv_chal, r.srv_chal.data, sizeof(srv_chal->data));
78 /****************************************************************************
81 Send the client credential, receive back a server credential.
82 Ensure that the server credential returned matches the session key
83 encrypt of the server challenge originally received. JRA.
84 ****************************************************************************/
86 NTSTATUS cli_net_auth2(struct cli_state *cli,
88 uint32 *neg_flags, DOM_CHAL *srv_chal)
90 prs_struct qbuf, rbuf;
93 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
96 prs_init(&qbuf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
97 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
99 if ( sec_chan == SEC_CHAN_DOMAIN )
100 fstr_sprintf( machine_acct, "%s$", lp_workgroup() );
102 fstrcpy( machine_acct, cli->mach_acct );
104 /* create and send a MSRPC command with api NET_AUTH2 */
106 DEBUG(4,("cli_net_auth2: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
107 cli->srv_name_slash, machine_acct, sec_chan, global_myname(),
108 credstr(cli->clnt_cred.challenge.data), *neg_flags));
110 /* store the parameters */
112 init_q_auth_2(&q, cli->srv_name_slash, machine_acct,
113 sec_chan, global_myname(), &cli->clnt_cred.challenge,
116 /* turn parameters into data stream */
118 if (!net_io_q_auth_2("", &q, &qbuf, 0) ||
119 !rpc_api_pipe_req(cli, PI_NETLOGON, NET_AUTH2, &qbuf, &rbuf)) {
123 /* Unmarshall response */
125 if (!net_io_r_auth_2("", &r, &rbuf, 0)) {
131 if (NT_STATUS_IS_OK(result)) {
135 * Check the returned value using the initial
136 * server received challenge.
140 if (cred_assert( &r.srv_chal, cli->sess_key, srv_chal,
144 * Server replied with bad credential. Fail.
146 DEBUG(0,("cli_net_auth2: server %s replied with bad credential (bad machine \
147 password ?).\n", cli->desthost ));
148 result = NT_STATUS_ACCESS_DENIED;
151 *neg_flags = r.srv_flgs.neg_flags;
161 /****************************************************************************
164 Send the client credential, receive back a server credential.
165 Ensure that the server credential returned matches the session key
166 encrypt of the server challenge originally received. JRA.
167 ****************************************************************************/
169 NTSTATUS cli_net_auth3(struct cli_state *cli,
171 uint32 *neg_flags, DOM_CHAL *srv_chal)
173 prs_struct qbuf, rbuf;
176 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
178 prs_init(&qbuf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
179 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
181 /* create and send a MSRPC command with api NET_AUTH2 */
183 DEBUG(4,("cli_net_auth3: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
184 cli->srv_name_slash, cli->mach_acct, sec_chan, global_myname(),
185 credstr(cli->clnt_cred.challenge.data), *neg_flags));
187 /* store the parameters */
188 init_q_auth_3(&q, cli->srv_name_slash, cli->mach_acct,
189 sec_chan, global_myname(), &cli->clnt_cred.challenge,
192 /* turn parameters into data stream */
194 if (!net_io_q_auth_3("", &q, &qbuf, 0) ||
195 !rpc_api_pipe_req(cli, PI_NETLOGON, NET_AUTH3, &qbuf, &rbuf)) {
199 /* Unmarshall response */
201 if (!net_io_r_auth_3("", &r, &rbuf, 0)) {
207 if (NT_STATUS_IS_OK(result)) {
211 * Check the returned value using the initial
212 * server received challenge.
216 if (cred_assert( &r.srv_chal, cli->sess_key, srv_chal,
220 * Server replied with bad credential. Fail.
222 DEBUG(0,("cli_net_auth3: server %s replied with bad credential (bad machine \
223 password ?).\n", cli->desthost ));
224 result = NT_STATUS_ACCESS_DENIED;
227 *neg_flags = r.srv_flgs.neg_flags;
237 /* Initialize domain session credentials */
239 NTSTATUS cli_nt_setup_creds(struct cli_state *cli,
241 const unsigned char mach_pwd[16], uint32 *neg_flags, int level)
248 /******************* Request Challenge ********************/
250 generate_random_buffer(clnt_chal.data, 8);
252 /* send a client challenge; receive a server challenge */
253 result = cli_net_req_chal(cli, &clnt_chal, &srv_chal);
255 if (!NT_STATUS_IS_OK(result)) {
256 DEBUG(0,("cli_nt_setup_creds: request challenge failed\n"));
260 /**************** Long-term Session key **************/
262 /* calculate the session key */
263 cred_session_key(&clnt_chal, &srv_chal, mach_pwd,
265 memset((char *)cli->sess_key+8, '\0', 8);
267 /******************* Authenticate 2/3 ********************/
269 /* calculate auth-2/3 credentials */
271 cred_create(cli->sess_key, &clnt_chal, zerotime, &cli->clnt_cred.challenge);
274 * Send client auth-2/3 challenge.
275 * Receive an auth-2/3 challenge response and check it.
279 result = cli_net_auth2(cli, sec_chan, neg_flags, &srv_chal);
282 result = cli_net_auth3(cli, sec_chan, neg_flags, &srv_chal);
285 DEBUG(1,("cli_nt_setup_creds: unsupported auth level: %d\n", level));
289 if (!NT_STATUS_IS_OK(result))
290 DEBUG(3,("cli_nt_setup_creds: auth%d challenge failed %s\n", level, nt_errstr(result)));
295 /* Logon Control 2 */
297 NTSTATUS cli_netlogon_logon_ctrl2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
300 prs_struct qbuf, rbuf;
303 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
308 /* Initialise parse structures */
310 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
311 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
313 /* Initialise input parameters */
315 init_net_q_logon_ctrl2(&q, cli->srv_name_slash, query_level);
317 /* Marshall data and send request */
319 if (!net_io_q_logon_ctrl2("", &q, &qbuf, 0) ||
320 !rpc_api_pipe_req(cli, PI_NETLOGON, NET_LOGON_CTRL2, &qbuf, &rbuf)) {
321 result = NT_STATUS_UNSUCCESSFUL;
325 /* Unmarshall response */
327 if (!net_io_r_logon_ctrl2("", &r, &rbuf, 0)) {
328 result = NT_STATUS_UNSUCCESSFUL;
343 NTSTATUS cli_netlogon_getdcname(struct cli_state *cli, TALLOC_CTX *mem_ctx,
344 const char *domainname, fstring dcname)
346 prs_struct qbuf, rbuf;
349 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
354 /* Initialise parse structures */
356 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
357 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
359 /* Initialise input parameters */
361 init_net_q_getdcname(&q, cli->srv_name_slash, domainname);
363 /* Marshall data and send request */
365 if (!net_io_q_getdcname("", &q, &qbuf, 0) ||
366 !rpc_api_pipe_req(cli, PI_NETLOGON, NET_GETDCNAME, &qbuf, &rbuf)) {
367 result = NT_STATUS_UNSUCCESSFUL;
371 /* Unmarshall response */
373 if (!net_io_r_getdcname("", &r, &rbuf, 0)) {
374 result = NT_STATUS_UNSUCCESSFUL;
380 if (NT_STATUS_IS_OK(result))
381 rpcstr_pull_unistr2_fstring(dcname, &r.uni_dcname);
390 /****************************************************************************
391 Generate the next creds to use.
392 ****************************************************************************/
394 static void gen_next_creds( struct cli_state *cli, DOM_CRED *new_clnt_cred)
397 * Create the new client credentials.
400 cli->clnt_cred.timestamp.time = time(NULL);
402 memcpy(new_clnt_cred, &cli->clnt_cred, sizeof(*new_clnt_cred));
404 /* Calculate the new credentials. */
405 cred_create(cli->sess_key, &(cli->clnt_cred.challenge),
406 new_clnt_cred->timestamp, &(new_clnt_cred->challenge));
409 /* Sam synchronisation */
411 NTSTATUS cli_netlogon_sam_sync(struct cli_state *cli, TALLOC_CTX *mem_ctx, DOM_CRED *ret_creds,
412 uint32 database_id, uint32 next_rid, uint32 *num_deltas,
413 SAM_DELTA_HDR **hdr_deltas,
414 SAM_DELTA_CTR **deltas)
416 prs_struct qbuf, rbuf;
419 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
425 /* Initialise parse structures */
427 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
428 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
430 /* Initialise input parameters */
432 gen_next_creds(cli, &clnt_creds);
434 init_net_q_sam_sync(&q, cli->srv_name_slash, cli->clnt_name_slash + 2,
435 &clnt_creds, ret_creds, database_id, next_rid);
437 /* Marshall data and send request */
439 if (!net_io_q_sam_sync("", &q, &qbuf, 0) ||
440 !rpc_api_pipe_req(cli, PI_NETLOGON, NET_SAM_SYNC, &qbuf, &rbuf)) {
441 result = NT_STATUS_UNSUCCESSFUL;
445 /* Unmarshall response */
447 if (!net_io_r_sam_sync("", cli->sess_key, &r, &rbuf, 0)) {
448 result = NT_STATUS_UNSUCCESSFUL;
455 *num_deltas = r.num_deltas2;
456 *hdr_deltas = r.hdr_deltas;
459 memcpy(ret_creds, &r.srv_creds, sizeof(*ret_creds));
468 /* Sam synchronisation */
470 NTSTATUS cli_netlogon_sam_deltas(struct cli_state *cli, TALLOC_CTX *mem_ctx,
471 uint32 database_id, UINT64_S seqnum,
473 SAM_DELTA_HDR **hdr_deltas,
474 SAM_DELTA_CTR **deltas)
476 prs_struct qbuf, rbuf;
479 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
485 /* Initialise parse structures */
487 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
488 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
490 /* Initialise input parameters */
492 gen_next_creds(cli, &clnt_creds);
494 init_net_q_sam_deltas(&q, cli->srv_name_slash,
495 cli->clnt_name_slash + 2, &clnt_creds,
496 database_id, seqnum);
498 /* Marshall data and send request */
500 if (!net_io_q_sam_deltas("", &q, &qbuf, 0) ||
501 !rpc_api_pipe_req(cli, PI_NETLOGON, NET_SAM_DELTAS, &qbuf, &rbuf)) {
502 result = NT_STATUS_UNSUCCESSFUL;
506 /* Unmarshall response */
508 if (!net_io_r_sam_deltas("", cli->sess_key, &r, &rbuf, 0)) {
509 result = NT_STATUS_UNSUCCESSFUL;
516 *num_deltas = r.num_deltas2;
517 *hdr_deltas = r.hdr_deltas;
527 /* Logon domain user */
529 NTSTATUS cli_netlogon_sam_logon(struct cli_state *cli, TALLOC_CTX *mem_ctx,
531 const char *username, const char *password,
534 prs_struct qbuf, rbuf;
537 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
538 DOM_CRED clnt_creds, dummy_rtn_creds;
540 NET_USER_INFO_3 user;
541 int validation_level = 3;
545 ZERO_STRUCT(dummy_rtn_creds);
547 /* Initialise parse structures */
549 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
550 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
552 /* Initialise input parameters */
554 gen_next_creds(cli, &clnt_creds);
556 q.validation_level = validation_level;
558 if (ret_creds == NULL)
559 ret_creds = &dummy_rtn_creds;
561 ctr.switch_value = logon_type;
563 switch (logon_type) {
564 case INTERACTIVE_LOGON_TYPE: {
565 unsigned char lm_owf_user_pwd[16], nt_owf_user_pwd[16];
567 nt_lm_owf_gen(password, nt_owf_user_pwd, lm_owf_user_pwd);
569 init_id_info1(&ctr.auth.id1, lp_workgroup(),
571 0xdead, 0xbeef, /* LUID? */
572 username, cli->clnt_name_slash,
573 (const char *)cli->sess_key, lm_owf_user_pwd,
578 case NET_LOGON_TYPE: {
580 unsigned char local_lm_response[24];
581 unsigned char local_nt_response[24];
583 generate_random_buffer(chal, 8);
585 SMBencrypt(password, chal, local_lm_response);
586 SMBNTencrypt(password, chal, local_nt_response);
588 init_id_info2(&ctr.auth.id2, lp_workgroup(),
590 0xdead, 0xbeef, /* LUID? */
591 username, cli->clnt_name_slash, chal,
592 local_lm_response, 24, local_nt_response, 24);
596 DEBUG(0, ("switch value %d not supported\n",
601 init_sam_info(&q.sam_id, cli->srv_name_slash, global_myname(),
602 &clnt_creds, ret_creds, logon_type,
605 /* Marshall data and send request */
607 if (!net_io_q_sam_logon("", &q, &qbuf, 0) ||
608 !rpc_api_pipe_req(cli, PI_NETLOGON, NET_SAMLOGON, &qbuf, &rbuf)) {
612 /* Unmarshall response */
616 if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
623 memcpy(ret_creds, &r.srv_creds, sizeof(*ret_creds));
634 * Logon domain user with an 'network' SAM logon
636 * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
639 NTSTATUS cli_netlogon_sam_network_logon(struct cli_state *cli, TALLOC_CTX *mem_ctx,
641 const char *username, const char *domain, const char *workstation,
643 DATA_BLOB lm_response, DATA_BLOB nt_response,
644 NET_USER_INFO_3 *info3)
647 prs_struct qbuf, rbuf;
650 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
651 DOM_CRED clnt_creds, dummy_rtn_creds;
653 int validation_level = 3;
654 char *workstation_name_slash;
655 uint8 netlogon_sess_key[16];
656 static uint8 zeros[16];
661 ZERO_STRUCT(dummy_rtn_creds);
663 workstation_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", workstation);
665 if (!workstation_name_slash) {
666 DEBUG(0, ("talloc_asprintf failed!\n"));
667 return NT_STATUS_NO_MEMORY;
670 /* Initialise parse structures */
672 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
673 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
675 /* Initialise input parameters */
677 gen_next_creds(cli, &clnt_creds);
679 q.validation_level = validation_level;
681 if (ret_creds == NULL)
682 ret_creds = &dummy_rtn_creds;
684 ctr.switch_value = NET_LOGON_TYPE;
686 init_id_info2(&ctr.auth.id2, domain,
688 0xdead, 0xbeef, /* LUID? */
689 username, workstation_name_slash, (const uchar*)chal,
690 lm_response.data, lm_response.length, nt_response.data, nt_response.length);
692 init_sam_info(&q.sam_id, cli->srv_name_slash, global_myname(),
693 &clnt_creds, ret_creds, NET_LOGON_TYPE,
696 /* Marshall data and send request */
698 if (!net_io_q_sam_logon("", &q, &qbuf, 0) ||
699 !rpc_api_pipe_req(cli, PI_NETLOGON, NET_SAMLOGON, &qbuf, &rbuf)) {
703 /* Unmarshall response */
707 if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
711 ZERO_STRUCT(netlogon_sess_key);
712 memcpy(netlogon_sess_key, cli->sess_key, 8);
714 if (memcmp(zeros, info3->user_sess_key, 16) != 0) {
715 SamOEMhash(info3->user_sess_key, netlogon_sess_key, 16);
717 memset(info3->user_sess_key, '\0', 16);
720 if (memcmp(zeros, info3->lm_sess_key, 8) != 0) {
721 SamOEMhash(info3->lm_sess_key, netlogon_sess_key, 8);
723 memset(info3->lm_sess_key, '\0', 8);
726 memset(&info3->acct_flags, '\0', 4);
727 for (i=0; i < 7; i++) {
728 memset(&info3->unknown[i], '\0', 4);
734 memcpy(ret_creds, &r.srv_creds, sizeof(*ret_creds));
743 /***************************************************************************
744 LSA Server Password Set.
745 ****************************************************************************/
747 NTSTATUS cli_net_srv_pwset(struct cli_state *cli, TALLOC_CTX *mem_ctx,
748 const char *machine_name, uint8 hashed_mach_pwd[16])
752 DOM_CRED new_clnt_cred;
754 uint16 sec_chan_type = 2;
757 gen_next_creds( cli, &new_clnt_cred);
759 prs_init(&qbuf , 1024, mem_ctx, MARSHALL);
760 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
762 DEBUG(4,("cli_net_srv_pwset: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
763 cli->srv_name_slash, cli->mach_acct, sec_chan_type, machine_name,
764 credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time));
766 /* store the parameters */
767 init_q_srv_pwset(&q_s, cli->srv_name_slash, (const char *)cli->sess_key,
768 cli->mach_acct, sec_chan_type, machine_name,
769 &new_clnt_cred, hashed_mach_pwd);
771 /* turn parameters into data stream */
772 if(!net_io_q_srv_pwset("", &q_s, &qbuf, 0)) {
773 DEBUG(0,("cli_net_srv_pwset: Error : failed to marshall NET_Q_SRV_PWSET struct.\n"));
774 nt_status = NT_STATUS_UNSUCCESSFUL;
778 /* send the data on \PIPE\ */
779 if (rpc_api_pipe_req(cli, PI_NETLOGON, NET_SRVPWSET, &qbuf, &rbuf))
783 if (!net_io_r_srv_pwset("", &r_s, &rbuf, 0)) {
784 nt_status = NT_STATUS_UNSUCCESSFUL;
788 nt_status = r_s.status;
790 if (!NT_STATUS_IS_OK(r_s.status))
792 /* report error code */
793 DEBUG(0,("cli_net_srv_pwset: %s\n", nt_errstr(nt_status)));
797 /* Update the credentials. */
798 if (!clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_cred)))
801 * Server replied with bad credential. Fail.
803 DEBUG(0,("cli_net_srv_pwset: server %s replied with bad credential (bad machine \
804 password ?).\n", cli->desthost ));
805 nt_status = NT_STATUS_UNSUCCESSFUL;