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
;
38 extern pstring global_myname
;
40 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, cli
->mem_ctx
, MARSHALL
);
41 prs_init(&rbuf
, 0, cli
->mem_ctx
, UNMARSHALL
);
43 /* create and send a MSRPC command with api NET_REQCHAL */
45 DEBUG(4,("cli_net_req_chal: LSA Request Challenge from %s to %s: %s\n",
46 global_myname
, cli
->desthost
, credstr(clnt_chal
->data
)));
48 /* store the parameters */
49 init_q_req_chal(&q
, cli
->srv_name_slash
, global_myname
, clnt_chal
);
51 /* Marshall data and send request */
53 if (!net_io_q_req_chal("", &q
, &qbuf
, 0) ||
54 !rpc_api_pipe_req(cli
, NET_REQCHAL
, &qbuf
, &rbuf
)) {
58 /* Unmarhall response */
60 if (!net_io_r_req_chal("", &r
, &rbuf
, 0)) {
68 if (NT_STATUS_IS_OK(result
)) {
69 memcpy(srv_chal
, r
.srv_chal
.data
, sizeof(srv_chal
->data
));
79 /****************************************************************************
82 Send the client credential, receive back a server credential.
83 Ensure that the server credential returned matches the session key
84 encrypt of the server challenge originally received. JRA.
85 ****************************************************************************/
87 NTSTATUS
cli_net_auth2(struct cli_state
*cli
,
89 uint32 neg_flags
, DOM_CHAL
*srv_chal
)
91 prs_struct qbuf
, rbuf
;
94 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
95 extern pstring global_myname
;
97 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, cli
->mem_ctx
, MARSHALL
);
98 prs_init(&rbuf
, 0, cli
->mem_ctx
, UNMARSHALL
);
100 /* create and send a MSRPC command with api NET_AUTH2 */
102 DEBUG(4,("cli_net_auth2: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
103 cli
->srv_name_slash
, cli
->mach_acct
, sec_chan
, global_myname
,
104 credstr(cli
->clnt_cred
.challenge
.data
), neg_flags
));
106 /* store the parameters */
107 init_q_auth_2(&q
, cli
->srv_name_slash
, cli
->mach_acct
,
108 sec_chan
, global_myname
, &cli
->clnt_cred
.challenge
,
111 /* turn parameters into data stream */
113 if (!net_io_q_auth_2("", &q
, &qbuf
, 0) ||
114 !rpc_api_pipe_req(cli
, NET_AUTH2
, &qbuf
, &rbuf
)) {
118 /* Unmarshall response */
120 if (!net_io_r_auth_2("", &r
, &rbuf
, 0)) {
126 if (NT_STATUS_IS_OK(result
)) {
130 * Check the returned value using the initial
131 * server received challenge.
135 if (cred_assert( &r
.srv_chal
, cli
->sess_key
, srv_chal
,
139 * Server replied with bad credential. Fail.
141 DEBUG(0,("cli_net_auth2: server %s replied with bad credential (bad machine \
142 password ?).\n", cli
->desthost
));
143 result
= NT_STATUS_ACCESS_DENIED
;
155 /****************************************************************************
158 Send the client credential, receive back a server credential.
159 Ensure that the server credential returned matches the session key
160 encrypt of the server challenge originally received. JRA.
161 ****************************************************************************/
163 NTSTATUS
cli_net_auth3(struct cli_state
*cli
,
165 uint32
*neg_flags
, DOM_CHAL
*srv_chal
)
167 prs_struct qbuf
, rbuf
;
170 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
171 extern pstring global_myname
;
173 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, cli
->mem_ctx
, MARSHALL
);
174 prs_init(&rbuf
, 0, cli
->mem_ctx
, UNMARSHALL
);
176 /* create and send a MSRPC command with api NET_AUTH2 */
178 DEBUG(4,("cli_net_auth3: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
179 cli
->srv_name_slash
, cli
->mach_acct
, sec_chan
, global_myname
,
180 credstr(cli
->clnt_cred
.challenge
.data
), *neg_flags
));
182 /* store the parameters */
183 init_q_auth_3(&q
, cli
->srv_name_slash
, cli
->mach_acct
,
184 sec_chan
, global_myname
, &cli
->clnt_cred
.challenge
,
187 /* turn parameters into data stream */
189 if (!net_io_q_auth_3("", &q
, &qbuf
, 0) ||
190 !rpc_api_pipe_req(cli
, NET_AUTH3
, &qbuf
, &rbuf
)) {
194 /* Unmarshall response */
196 if (!net_io_r_auth_3("", &r
, &rbuf
, 0)) {
201 *neg_flags
= r
.srv_flgs
.neg_flags
;
203 if (NT_STATUS_IS_OK(result
)) {
207 * Check the returned value using the initial
208 * server received challenge.
212 if (cred_assert( &r
.srv_chal
, cli
->sess_key
, srv_chal
,
216 * Server replied with bad credential. Fail.
218 DEBUG(0,("cli_net_auth3: server %s replied with bad credential (bad machine \
219 password ?).\n", cli
->desthost
));
220 result
= NT_STATUS_ACCESS_DENIED
;
232 /* Return the secure channel type depending on the server role. */
234 uint16
get_sec_chan(void)
236 uint16 sec_chan
= SEC_CHAN_WKSTA
;
238 switch (lp_server_role()) {
239 case ROLE_DOMAIN_PDC
:
240 sec_chan
= SEC_CHAN_DOMAIN
;
242 case ROLE_DOMAIN_BDC
:
243 sec_chan
= SEC_CHAN_BDC
;
250 /* Initialize domain session credentials */
252 NTSTATUS
cli_nt_setup_creds(struct cli_state
*cli
,
254 const unsigned char mach_pwd
[16], uint32
*neg_flags
, int level
)
261 /******************* Request Challenge ********************/
263 generate_random_buffer(clnt_chal
.data
, 8, False
);
265 /* send a client challenge; receive a server challenge */
266 result
= cli_net_req_chal(cli
, &clnt_chal
, &srv_chal
);
268 if (!NT_STATUS_IS_OK(result
)) {
269 DEBUG(0,("cli_nt_setup_creds: request challenge failed\n"));
273 /**************** Long-term Session key **************/
275 /* calculate the session key */
276 cred_session_key(&clnt_chal
, &srv_chal
, mach_pwd
,
278 memset((char *)cli
->sess_key
+8, '\0', 8);
280 /******************* Authenticate 2/3 ********************/
282 /* calculate auth-2/3 credentials */
284 cred_create(cli
->sess_key
, &clnt_chal
, zerotime
, &cli
->clnt_cred
.challenge
);
287 * Send client auth-2/3 challenge.
288 * Receive an auth-2/3 challenge response and check it.
292 result
= cli_net_auth2(cli
, sec_chan
, *neg_flags
, &srv_chal
);
295 result
= cli_net_auth3(cli
, sec_chan
, neg_flags
, &srv_chal
);
298 DEBUG(1,("cli_nt_setup_creds: unsupported auth level: %d\n", level
));
302 if (!NT_STATUS_IS_OK(result
))
303 DEBUG(1,("cli_nt_setup_creds: auth%d challenge failed %s\n", level
, nt_errstr(result
)));
308 /* Logon Control 2 */
310 NTSTATUS
cli_netlogon_logon_ctrl2(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
313 prs_struct qbuf
, rbuf
;
316 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
321 /* Initialise parse structures */
323 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
324 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
326 /* Initialise input parameters */
328 init_net_q_logon_ctrl2(&q
, cli
->srv_name_slash
, query_level
);
330 /* Marshall data and send request */
332 if (!net_io_q_logon_ctrl2("", &q
, &qbuf
, 0) ||
333 !rpc_api_pipe_req(cli
, NET_LOGON_CTRL2
, &qbuf
, &rbuf
)) {
334 result
= NT_STATUS_UNSUCCESSFUL
;
338 /* Unmarshall response */
340 if (!net_io_r_logon_ctrl2("", &r
, &rbuf
, 0)) {
341 result
= NT_STATUS_UNSUCCESSFUL
;
354 /****************************************************************************
355 Generate the next creds to use. Yuck - this is a cut&paste from another
356 file. They should be combined at some stage. )-:
357 ****************************************************************************/
359 static void gen_next_creds( struct cli_state
*cli
, DOM_CRED
*new_clnt_cred
)
362 * Create the new client credentials.
365 cli
->clnt_cred
.timestamp
.time
= time(NULL
);
367 memcpy(new_clnt_cred
, &cli
->clnt_cred
, sizeof(*new_clnt_cred
));
369 /* Calculate the new credentials. */
370 cred_create(cli
->sess_key
, &(cli
->clnt_cred
.challenge
),
371 new_clnt_cred
->timestamp
, &(new_clnt_cred
->challenge
));
374 /* Sam synchronisation */
376 NTSTATUS
cli_netlogon_sam_sync(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
, DOM_CRED
*ret_creds
,
377 uint32 database_id
, uint32 next_rid
, uint32
*num_deltas
,
378 SAM_DELTA_HDR
**hdr_deltas
,
379 SAM_DELTA_CTR
**deltas
)
381 prs_struct qbuf
, rbuf
;
384 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
390 /* Initialise parse structures */
392 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
393 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
395 /* Initialise input parameters */
397 gen_next_creds(cli
, &clnt_creds
);
399 init_net_q_sam_sync(&q
, cli
->srv_name_slash
, cli
->clnt_name_slash
+ 2,
400 &clnt_creds
, ret_creds
, database_id
, next_rid
);
402 /* Marshall data and send request */
404 if (!net_io_q_sam_sync("", &q
, &qbuf
, 0) ||
405 !rpc_api_pipe_req(cli
, NET_SAM_SYNC
, &qbuf
, &rbuf
)) {
406 result
= NT_STATUS_UNSUCCESSFUL
;
410 /* Unmarshall response */
412 if (!net_io_r_sam_sync("", cli
->sess_key
, &r
, &rbuf
, 0)) {
413 result
= NT_STATUS_UNSUCCESSFUL
;
420 *num_deltas
= r
.num_deltas2
;
421 *hdr_deltas
= r
.hdr_deltas
;
424 memcpy(ret_creds
, &r
.srv_creds
, sizeof(*ret_creds
));
433 /* Sam synchronisation */
435 NTSTATUS
cli_netlogon_sam_deltas(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
436 uint32 database_id
, UINT64_S seqnum
,
438 SAM_DELTA_HDR
**hdr_deltas
,
439 SAM_DELTA_CTR
**deltas
)
441 prs_struct qbuf
, rbuf
;
444 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
450 /* Initialise parse structures */
452 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
453 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
455 /* Initialise input parameters */
457 gen_next_creds(cli
, &clnt_creds
);
459 init_net_q_sam_deltas(&q
, cli
->srv_name_slash
,
460 cli
->clnt_name_slash
+ 2, &clnt_creds
,
461 database_id
, seqnum
);
463 /* Marshall data and send request */
465 if (!net_io_q_sam_deltas("", &q
, &qbuf
, 0) ||
466 !rpc_api_pipe_req(cli
, NET_SAM_DELTAS
, &qbuf
, &rbuf
)) {
467 result
= NT_STATUS_UNSUCCESSFUL
;
471 /* Unmarshall response */
473 if (!net_io_r_sam_deltas("", cli
->sess_key
, &r
, &rbuf
, 0)) {
474 result
= NT_STATUS_UNSUCCESSFUL
;
481 *num_deltas
= r
.num_deltas2
;
482 *hdr_deltas
= r
.hdr_deltas
;
492 /* Logon domain user */
494 NTSTATUS
cli_netlogon_sam_logon(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
495 char *username
, char *password
,
498 prs_struct qbuf
, rbuf
;
501 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
502 DOM_CRED clnt_creds
, dummy_rtn_creds
;
503 extern pstring global_myname
;
505 NET_USER_INFO_3 user
;
506 int validation_level
= 3;
511 /* Initialise parse structures */
513 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
514 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
516 /* Initialise input parameters */
518 gen_next_creds(cli
, &clnt_creds
);
520 q
.validation_level
= validation_level
;
522 memset(&dummy_rtn_creds
, '\0', sizeof(dummy_rtn_creds
));
523 dummy_rtn_creds
.timestamp
.time
= time(NULL
);
525 ctr
.switch_value
= logon_type
;
527 switch (logon_type
) {
528 case INTERACTIVE_LOGON_TYPE
: {
529 unsigned char lm_owf_user_pwd
[16], nt_owf_user_pwd
[16];
531 nt_lm_owf_gen(password
, nt_owf_user_pwd
, lm_owf_user_pwd
);
533 init_id_info1(&ctr
.auth
.id1
, lp_workgroup(),
535 0xdead, 0xbeef, /* LUID? */
536 username
, cli
->clnt_name_slash
,
537 cli
->sess_key
, lm_owf_user_pwd
,
542 case NET_LOGON_TYPE
: {
544 unsigned char local_lm_response
[24];
545 unsigned char local_nt_response
[24];
547 generate_random_buffer(chal
, 8, False
);
549 SMBencrypt(password
, chal
, local_lm_response
);
550 SMBNTencrypt(password
, chal
, local_nt_response
);
552 init_id_info2(&ctr
.auth
.id2
, lp_workgroup(),
554 0xdead, 0xbeef, /* LUID? */
555 username
, cli
->clnt_name_slash
, chal
,
556 local_lm_response
, 24, local_nt_response
, 24);
560 DEBUG(0, ("switch value %d not supported\n",
565 init_sam_info(&q
.sam_id
, cli
->srv_name_slash
, global_myname
,
566 &clnt_creds
, &dummy_rtn_creds
, logon_type
,
569 /* Marshall data and send request */
571 if (!net_io_q_sam_logon("", &q
, &qbuf
, 0) ||
572 !rpc_api_pipe_req(cli
, NET_SAMLOGON
, &qbuf
, &rbuf
)) {
576 /* Unmarshall response */
580 if (!net_io_r_sam_logon("", &r
, &rbuf
, 0)) {
597 * Logon domain user with an 'network' SAM logon
599 * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
602 NTSTATUS
cli_netlogon_sam_network_logon(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
603 const char *username
, const char *domain
, const char *workstation
,
605 DATA_BLOB lm_response
, DATA_BLOB nt_response
,
606 NET_USER_INFO_3
*info3
)
609 prs_struct qbuf
, rbuf
;
612 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
613 DOM_CRED clnt_creds
, dummy_rtn_creds
;
615 extern pstring global_myname
;
616 int validation_level
= 3;
617 char *workstation_name_slash
;
622 workstation_name_slash
= talloc_asprintf(mem_ctx
, "\\\\%s", workstation
);
624 if (!workstation_name_slash
) {
625 DEBUG(0, ("talloc_asprintf failed!\n"));
626 return NT_STATUS_NO_MEMORY
;
629 /* Initialise parse structures */
631 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
632 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
634 /* Initialise input parameters */
636 gen_next_creds(cli
, &clnt_creds
);
638 q
.validation_level
= validation_level
;
640 memset(&dummy_rtn_creds
, '\0', sizeof(dummy_rtn_creds
));
641 dummy_rtn_creds
.timestamp
.time
= time(NULL
);
643 ctr
.switch_value
= NET_LOGON_TYPE
;
645 init_id_info2(&ctr
.auth
.id2
, domain
,
647 0xdead, 0xbeef, /* LUID? */
648 username
, workstation_name_slash
, (const uchar
*)chal
,
649 lm_response
.data
, lm_response
.length
, nt_response
.data
, nt_response
.length
);
651 init_sam_info(&q
.sam_id
, cli
->srv_name_slash
, global_myname
,
652 &clnt_creds
, &dummy_rtn_creds
, NET_LOGON_TYPE
,
655 /* Marshall data and send request */
657 if (!net_io_q_sam_logon("", &q
, &qbuf
, 0) ||
658 !rpc_api_pipe_req(cli
, NET_SAMLOGON
, &qbuf
, &rbuf
)) {
662 /* Unmarshall response */
666 if (!net_io_r_sam_logon("", &r
, &rbuf
, 0)) {
681 /***************************************************************************
682 LSA Server Password Set.
683 ****************************************************************************/
685 NTSTATUS
cli_net_srv_pwset(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
686 char* machine_name
, uint8 hashed_mach_pwd
[16])
690 DOM_CRED new_clnt_cred
;
692 uint16 sec_chan_type
= 2;
696 gen_next_creds( cli
, &new_clnt_cred
);
698 prs_init(&qbuf
, 1024, mem_ctx
, MARSHALL
);
699 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
701 /* create and send a MSRPC command with api NET_SRV_PWSET */
703 mach_acct
= talloc_asprintf(mem_ctx
, "%s$", machine_name
);
706 DEBUG(0,("talloc_asprintf failed!\n"));
707 nt_status
= NT_STATUS_NO_MEMORY
;
711 DEBUG(4,("cli_net_srv_pwset: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
712 cli
->srv_name_slash
, mach_acct
, sec_chan_type
, machine_name
,
713 credstr(new_clnt_cred
.challenge
.data
), new_clnt_cred
.timestamp
.time
));
715 /* store the parameters */
716 init_q_srv_pwset(&q_s
, cli
->srv_name_slash
, cli
->sess_key
,
717 mach_acct
, sec_chan_type
, machine_name
,
718 &new_clnt_cred
, (char *)hashed_mach_pwd
);
720 /* turn parameters into data stream */
721 if(!net_io_q_srv_pwset("", &q_s
, &qbuf
, 0)) {
722 DEBUG(0,("cli_net_srv_pwset: Error : failed to marshall NET_Q_SRV_PWSET struct.\n"));
723 nt_status
= NT_STATUS_UNSUCCESSFUL
;
727 /* send the data on \PIPE\ */
728 if (rpc_api_pipe_req(cli
, NET_SRVPWSET
, &qbuf
, &rbuf
))
732 if (!net_io_r_srv_pwset("", &r_s
, &rbuf
, 0)) {
733 nt_status
= NT_STATUS_UNSUCCESSFUL
;
737 nt_status
= r_s
.status
;
739 if (!NT_STATUS_IS_OK(r_s
.status
))
741 /* report error code */
742 DEBUG(0,("cli_net_srv_pwset: %s\n", nt_errstr(nt_status
)));
746 /* Update the credentials. */
747 if (!clnt_deal_with_creds(cli
->sess_key
, &(cli
->clnt_cred
), &(r_s
.srv_cred
)))
750 * Server replied with bad credential. Fail.
752 DEBUG(0,("cli_net_srv_pwset: server %s replied with bad credential (bad machine \
753 password ?).\n", cli
->desthost
));
754 nt_status
= NT_STATUS_UNSUCCESSFUL
;