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, 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;
95 prs_init(&qbuf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
96 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
98 /* create and send a MSRPC command with api NET_AUTH2 */
100 DEBUG(4,("cli_net_auth2: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
101 cli->srv_name_slash, cli->mach_acct, sec_chan, global_myname(),
102 credstr(cli->clnt_cred.challenge.data), *neg_flags));
104 /* store the parameters */
105 init_q_auth_2(&q, cli->srv_name_slash, cli->mach_acct,
106 sec_chan, global_myname(), &cli->clnt_cred.challenge,
109 /* turn parameters into data stream */
111 if (!net_io_q_auth_2("", &q, &qbuf, 0) ||
112 !rpc_api_pipe_req(cli, NET_AUTH2, &qbuf, &rbuf)) {
116 /* Unmarshall response */
118 if (!net_io_r_auth_2("", &r, &rbuf, 0)) {
124 if (NT_STATUS_IS_OK(result)) {
128 * Check the returned value using the initial
129 * server received challenge.
133 if (cred_assert( &r.srv_chal, cli->sess_key, srv_chal,
137 * Server replied with bad credential. Fail.
139 DEBUG(0,("cli_net_auth2: server %s replied with bad credential (bad machine \
140 password ?).\n", cli->desthost ));
141 result = NT_STATUS_ACCESS_DENIED;
144 *neg_flags = r.srv_flgs.neg_flags;
154 /****************************************************************************
157 Send the client credential, receive back a server credential.
158 Ensure that the server credential returned matches the session key
159 encrypt of the server challenge originally received. JRA.
160 ****************************************************************************/
162 NTSTATUS cli_net_auth3(struct cli_state *cli,
164 uint32 *neg_flags, DOM_CHAL *srv_chal)
166 prs_struct qbuf, rbuf;
169 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
171 prs_init(&qbuf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
172 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
174 /* create and send a MSRPC command with api NET_AUTH2 */
176 DEBUG(4,("cli_net_auth3: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
177 cli->srv_name_slash, cli->mach_acct, sec_chan, global_myname(),
178 credstr(cli->clnt_cred.challenge.data), *neg_flags));
180 /* store the parameters */
181 init_q_auth_3(&q, cli->srv_name_slash, cli->mach_acct,
182 sec_chan, global_myname(), &cli->clnt_cred.challenge,
185 /* turn parameters into data stream */
187 if (!net_io_q_auth_3("", &q, &qbuf, 0) ||
188 !rpc_api_pipe_req(cli, NET_AUTH3, &qbuf, &rbuf)) {
192 /* Unmarshall response */
194 if (!net_io_r_auth_3("", &r, &rbuf, 0)) {
200 if (NT_STATUS_IS_OK(result)) {
204 * Check the returned value using the initial
205 * server received challenge.
209 if (cred_assert( &r.srv_chal, cli->sess_key, srv_chal,
213 * Server replied with bad credential. Fail.
215 DEBUG(0,("cli_net_auth3: server %s replied with bad credential (bad machine \
216 password ?).\n", cli->desthost ));
217 result = NT_STATUS_ACCESS_DENIED;
220 *neg_flags = r.srv_flgs.neg_flags;
230 /* Initialize domain session credentials */
232 NTSTATUS cli_nt_setup_creds(struct cli_state *cli,
234 const unsigned char mach_pwd[16], uint32 *neg_flags, int level)
241 /******************* Request Challenge ********************/
243 generate_random_buffer(clnt_chal.data, 8, False);
245 /* send a client challenge; receive a server challenge */
246 result = cli_net_req_chal(cli, &clnt_chal, &srv_chal);
248 if (!NT_STATUS_IS_OK(result)) {
249 DEBUG(0,("cli_nt_setup_creds: request challenge failed\n"));
253 /**************** Long-term Session key **************/
255 /* calculate the session key */
256 cred_session_key(&clnt_chal, &srv_chal, mach_pwd,
258 memset((char *)cli->sess_key+8, '\0', 8);
260 /******************* Authenticate 2/3 ********************/
262 /* calculate auth-2/3 credentials */
264 cred_create(cli->sess_key, &clnt_chal, zerotime, &cli->clnt_cred.challenge);
267 * Send client auth-2/3 challenge.
268 * Receive an auth-2/3 challenge response and check it.
272 result = cli_net_auth2(cli, sec_chan, neg_flags, &srv_chal);
275 result = cli_net_auth3(cli, sec_chan, neg_flags, &srv_chal);
278 DEBUG(1,("cli_nt_setup_creds: unsupported auth level: %d\n", level));
282 if (!NT_STATUS_IS_OK(result))
283 DEBUG(3,("cli_nt_setup_creds: auth%d challenge failed %s\n", level, nt_errstr(result)));
288 /* Logon Control 2 */
290 NTSTATUS cli_netlogon_logon_ctrl2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
293 prs_struct qbuf, rbuf;
296 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
301 /* Initialise parse structures */
303 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
304 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
306 /* Initialise input parameters */
308 init_net_q_logon_ctrl2(&q, cli->srv_name_slash, query_level);
310 /* Marshall data and send request */
312 if (!net_io_q_logon_ctrl2("", &q, &qbuf, 0) ||
313 !rpc_api_pipe_req(cli, NET_LOGON_CTRL2, &qbuf, &rbuf)) {
314 result = NT_STATUS_UNSUCCESSFUL;
318 /* Unmarshall response */
320 if (!net_io_r_logon_ctrl2("", &r, &rbuf, 0)) {
321 result = NT_STATUS_UNSUCCESSFUL;
334 /****************************************************************************
335 Generate the next creds to use. Yuck - this is a cut&paste from another
336 file. They should be combined at some stage. )-:
337 ****************************************************************************/
339 static void gen_next_creds( struct cli_state *cli, DOM_CRED *new_clnt_cred)
342 * Create the new client credentials.
345 cli->clnt_cred.timestamp.time = time(NULL);
347 memcpy(new_clnt_cred, &cli->clnt_cred, sizeof(*new_clnt_cred));
349 /* Calculate the new credentials. */
350 cred_create(cli->sess_key, &(cli->clnt_cred.challenge),
351 new_clnt_cred->timestamp, &(new_clnt_cred->challenge));
354 /* Sam synchronisation */
356 NTSTATUS cli_netlogon_sam_sync(struct cli_state *cli, TALLOC_CTX *mem_ctx, DOM_CRED *ret_creds,
357 uint32 database_id, uint32 next_rid, uint32 *num_deltas,
358 SAM_DELTA_HDR **hdr_deltas,
359 SAM_DELTA_CTR **deltas)
361 prs_struct qbuf, rbuf;
364 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
370 /* Initialise parse structures */
372 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
373 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
375 /* Initialise input parameters */
377 gen_next_creds(cli, &clnt_creds);
379 init_net_q_sam_sync(&q, cli->srv_name_slash, cli->clnt_name_slash + 2,
380 &clnt_creds, ret_creds, database_id, next_rid);
382 /* Marshall data and send request */
384 if (!net_io_q_sam_sync("", &q, &qbuf, 0) ||
385 !rpc_api_pipe_req(cli, NET_SAM_SYNC, &qbuf, &rbuf)) {
386 result = NT_STATUS_UNSUCCESSFUL;
390 /* Unmarshall response */
392 if (!net_io_r_sam_sync("", cli->sess_key, &r, &rbuf, 0)) {
393 result = NT_STATUS_UNSUCCESSFUL;
400 *num_deltas = r.num_deltas2;
401 *hdr_deltas = r.hdr_deltas;
404 memcpy(ret_creds, &r.srv_creds, sizeof(*ret_creds));
413 /* Sam synchronisation */
415 NTSTATUS cli_netlogon_sam_deltas(struct cli_state *cli, TALLOC_CTX *mem_ctx,
416 uint32 database_id, UINT64_S seqnum,
418 SAM_DELTA_HDR **hdr_deltas,
419 SAM_DELTA_CTR **deltas)
421 prs_struct qbuf, rbuf;
424 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
430 /* Initialise parse structures */
432 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
433 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
435 /* Initialise input parameters */
437 gen_next_creds(cli, &clnt_creds);
439 init_net_q_sam_deltas(&q, cli->srv_name_slash,
440 cli->clnt_name_slash + 2, &clnt_creds,
441 database_id, seqnum);
443 /* Marshall data and send request */
445 if (!net_io_q_sam_deltas("", &q, &qbuf, 0) ||
446 !rpc_api_pipe_req(cli, NET_SAM_DELTAS, &qbuf, &rbuf)) {
447 result = NT_STATUS_UNSUCCESSFUL;
451 /* Unmarshall response */
453 if (!net_io_r_sam_deltas("", cli->sess_key, &r, &rbuf, 0)) {
454 result = NT_STATUS_UNSUCCESSFUL;
461 *num_deltas = r.num_deltas2;
462 *hdr_deltas = r.hdr_deltas;
472 /* Logon domain user */
474 NTSTATUS cli_netlogon_sam_logon(struct cli_state *cli, TALLOC_CTX *mem_ctx,
475 const char *username, const char *password,
478 prs_struct qbuf, rbuf;
481 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
482 DOM_CRED clnt_creds, dummy_rtn_creds;
484 NET_USER_INFO_3 user;
485 int validation_level = 3;
490 /* Initialise parse structures */
492 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
493 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
495 /* Initialise input parameters */
497 gen_next_creds(cli, &clnt_creds);
499 q.validation_level = validation_level;
501 memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
502 dummy_rtn_creds.timestamp.time = time(NULL);
504 ctr.switch_value = logon_type;
506 switch (logon_type) {
507 case INTERACTIVE_LOGON_TYPE: {
508 unsigned char lm_owf_user_pwd[16], nt_owf_user_pwd[16];
510 nt_lm_owf_gen(password, nt_owf_user_pwd, lm_owf_user_pwd);
512 init_id_info1(&ctr.auth.id1, lp_workgroup(),
514 0xdead, 0xbeef, /* LUID? */
515 username, cli->clnt_name_slash,
516 cli->sess_key, lm_owf_user_pwd,
521 case NET_LOGON_TYPE: {
523 unsigned char local_lm_response[24];
524 unsigned char local_nt_response[24];
526 generate_random_buffer(chal, 8, False);
528 SMBencrypt(password, chal, local_lm_response);
529 SMBNTencrypt(password, chal, local_nt_response);
531 init_id_info2(&ctr.auth.id2, lp_workgroup(),
533 0xdead, 0xbeef, /* LUID? */
534 username, cli->clnt_name_slash, chal,
535 local_lm_response, 24, local_nt_response, 24);
539 DEBUG(0, ("switch value %d not supported\n",
544 init_sam_info(&q.sam_id, cli->srv_name_slash, global_myname(),
545 &clnt_creds, &dummy_rtn_creds, logon_type,
548 /* Marshall data and send request */
550 if (!net_io_q_sam_logon("", &q, &qbuf, 0) ||
551 !rpc_api_pipe_req(cli, NET_SAMLOGON, &qbuf, &rbuf)) {
555 /* Unmarshall response */
559 if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
576 * Logon domain user with an 'network' SAM logon
578 * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
581 NTSTATUS cli_netlogon_sam_network_logon(struct cli_state *cli, TALLOC_CTX *mem_ctx,
582 const char *username, const char *domain, const char *workstation,
584 DATA_BLOB lm_response, DATA_BLOB nt_response,
585 NET_USER_INFO_3 *info3)
588 prs_struct qbuf, rbuf;
591 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
592 DOM_CRED clnt_creds, dummy_rtn_creds;
594 int validation_level = 3;
595 char *workstation_name_slash;
596 uint8 netlogon_sess_key[16];
597 static uint8 zeros[16];
602 workstation_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", workstation);
604 if (!workstation_name_slash) {
605 DEBUG(0, ("talloc_asprintf failed!\n"));
606 return NT_STATUS_NO_MEMORY;
609 /* Initialise parse structures */
611 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
612 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
614 /* Initialise input parameters */
616 gen_next_creds(cli, &clnt_creds);
618 q.validation_level = validation_level;
620 memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
621 dummy_rtn_creds.timestamp.time = time(NULL);
623 ctr.switch_value = NET_LOGON_TYPE;
625 init_id_info2(&ctr.auth.id2, domain,
627 0xdead, 0xbeef, /* LUID? */
628 username, workstation_name_slash, (const uchar*)chal,
629 lm_response.data, lm_response.length, nt_response.data, nt_response.length);
631 init_sam_info(&q.sam_id, cli->srv_name_slash, global_myname(),
632 &clnt_creds, &dummy_rtn_creds, NET_LOGON_TYPE,
635 /* Marshall data and send request */
637 if (!net_io_q_sam_logon("", &q, &qbuf, 0) ||
638 !rpc_api_pipe_req(cli, NET_SAMLOGON, &qbuf, &rbuf)) {
642 /* Unmarshall response */
646 if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
650 ZERO_STRUCT(netlogon_sess_key);
651 memcpy(netlogon_sess_key, cli->sess_key, 8);
653 if (memcmp(zeros, info3->user_sess_key, 16) != 0)
654 SamOEMhash(info3->user_sess_key, netlogon_sess_key, 16);
656 if (memcmp(zeros, info3->padding, 16) != 0)
657 SamOEMhash(info3->padding, netlogon_sess_key, 16);
670 /***************************************************************************
671 LSA Server Password Set.
672 ****************************************************************************/
674 NTSTATUS cli_net_srv_pwset(struct cli_state *cli, TALLOC_CTX *mem_ctx,
675 const char *machine_name, uint8 hashed_mach_pwd[16])
679 DOM_CRED new_clnt_cred;
681 uint16 sec_chan_type = 2;
685 gen_next_creds( cli, &new_clnt_cred);
687 prs_init(&qbuf , 1024, mem_ctx, MARSHALL);
688 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
690 /* create and send a MSRPC command with api NET_SRV_PWSET */
692 mach_acct = talloc_asprintf(mem_ctx, "%s$", machine_name);
695 DEBUG(0,("talloc_asprintf failed!\n"));
696 nt_status = NT_STATUS_NO_MEMORY;
700 DEBUG(4,("cli_net_srv_pwset: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
701 cli->srv_name_slash, mach_acct, sec_chan_type, machine_name,
702 credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time));
704 /* store the parameters */
705 init_q_srv_pwset(&q_s, cli->srv_name_slash, cli->sess_key,
706 mach_acct, sec_chan_type, machine_name,
707 &new_clnt_cred, (char *)hashed_mach_pwd);
709 /* turn parameters into data stream */
710 if(!net_io_q_srv_pwset("", &q_s, &qbuf, 0)) {
711 DEBUG(0,("cli_net_srv_pwset: Error : failed to marshall NET_Q_SRV_PWSET struct.\n"));
712 nt_status = NT_STATUS_UNSUCCESSFUL;
716 /* send the data on \PIPE\ */
717 if (rpc_api_pipe_req(cli, NET_SRVPWSET, &qbuf, &rbuf))
721 if (!net_io_r_srv_pwset("", &r_s, &rbuf, 0)) {
722 nt_status = NT_STATUS_UNSUCCESSFUL;
726 nt_status = r_s.status;
728 if (!NT_STATUS_IS_OK(r_s.status))
730 /* report error code */
731 DEBUG(0,("cli_net_srv_pwset: %s\n", nt_errstr(nt_status)));
735 /* Update the credentials. */
736 if (!clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_cred)))
739 * Server replied with bad credential. Fail.
741 DEBUG(0,("cli_net_srv_pwset: server %s replied with bad credential (bad machine \
742 password ?).\n", cli->desthost ));
743 nt_status = NT_STATUS_UNSUCCESSFUL;