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 /* Initialize domain session credentials */
157 NTSTATUS
cli_nt_setup_creds(struct cli_state
*cli
,
159 const unsigned char mach_pwd
[16])
166 /******************* Request Challenge ********************/
168 generate_random_buffer(clnt_chal
.data
, 8, False
);
170 /* send a client challenge; receive a server challenge */
171 result
= cli_net_req_chal(cli
, &clnt_chal
, &srv_chal
);
173 if (!NT_STATUS_IS_OK(result
)) {
174 DEBUG(0,("cli_nt_setup_creds: request challenge failed\n"));
178 /**************** Long-term Session key **************/
180 /* calculate the session key */
181 cred_session_key(&clnt_chal
, &srv_chal
, mach_pwd
,
183 memset((char *)cli
->sess_key
+8, '\0', 8);
185 /******************* Authenticate 2 ********************/
187 /* calculate auth-2 credentials */
189 cred_create(cli
->sess_key
, &clnt_chal
, zerotime
,
190 &cli
->clnt_cred
.challenge
);
193 * Send client auth-2 challenge.
194 * Receive an auth-2 challenge response and check it.
197 result
= cli_net_auth2(cli
, sec_chan
, 0x000001ff, &srv_chal
);
199 if (!NT_STATUS_IS_OK(result
)) {
200 DEBUG(1,("cli_nt_setup_creds: auth2 challenge failed %s\n",
207 /* Logon Control 2 */
209 NTSTATUS
cli_netlogon_logon_ctrl2(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
212 prs_struct qbuf
, rbuf
;
215 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
220 /* Initialise parse structures */
222 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
223 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
225 /* Initialise input parameters */
227 init_net_q_logon_ctrl2(&q
, cli
->srv_name_slash
, query_level
);
229 /* Marshall data and send request */
231 if (!net_io_q_logon_ctrl2("", &q
, &qbuf
, 0) ||
232 !rpc_api_pipe_req(cli
, NET_LOGON_CTRL2
, &qbuf
, &rbuf
)) {
233 result
= NT_STATUS_UNSUCCESSFUL
;
237 /* Unmarshall response */
239 if (!net_io_r_logon_ctrl2("", &r
, &rbuf
, 0)) {
240 result
= NT_STATUS_UNSUCCESSFUL
;
253 /****************************************************************************
254 Generate the next creds to use. Yuck - this is a cut&paste from another
255 file. They should be combined at some stage. )-:
256 ****************************************************************************/
258 static void gen_next_creds( struct cli_state
*cli
, DOM_CRED
*new_clnt_cred
)
261 * Create the new client credentials.
264 cli
->clnt_cred
.timestamp
.time
= time(NULL
);
266 memcpy(new_clnt_cred
, &cli
->clnt_cred
, sizeof(*new_clnt_cred
));
268 /* Calculate the new credentials. */
269 cred_create(cli
->sess_key
, &(cli
->clnt_cred
.challenge
),
270 new_clnt_cred
->timestamp
, &(new_clnt_cred
->challenge
));
274 /* Sam synchronisation */
276 NTSTATUS
cli_netlogon_sam_sync(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
, DOM_CRED
*ret_creds
,
277 uint32 database_id
, uint32
*num_deltas
,
278 SAM_DELTA_HDR
**hdr_deltas
,
279 SAM_DELTA_CTR
**deltas
)
281 prs_struct qbuf
, rbuf
;
284 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
290 /* Initialise parse structures */
292 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
293 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
295 /* Initialise input parameters */
297 gen_next_creds(cli
, &clnt_creds
);
299 init_net_q_sam_sync(&q
, cli
->srv_name_slash
, cli
->clnt_name_slash
+ 2,
300 &clnt_creds
, ret_creds
, database_id
);
302 /* Marshall data and send request */
304 if (!net_io_q_sam_sync("", &q
, &qbuf
, 0) ||
305 !rpc_api_pipe_req(cli
, NET_SAM_SYNC
, &qbuf
, &rbuf
)) {
306 result
= NT_STATUS_UNSUCCESSFUL
;
310 /* Unmarshall response */
312 if (!net_io_r_sam_sync("", cli
->sess_key
, &r
, &rbuf
, 0)) {
313 result
= NT_STATUS_UNSUCCESSFUL
;
320 *num_deltas
= r
.num_deltas2
;
321 *hdr_deltas
= r
.hdr_deltas
;
324 memcpy(ret_creds
, &r
.srv_creds
, sizeof(*ret_creds
));
333 /* Sam synchronisation */
335 NTSTATUS
cli_netlogon_sam_deltas(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
336 uint32 database_id
, UINT64_S seqnum
,
338 SAM_DELTA_HDR
**hdr_deltas
,
339 SAM_DELTA_CTR
**deltas
)
341 prs_struct qbuf
, rbuf
;
344 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
350 /* Initialise parse structures */
352 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
353 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
355 /* Initialise input parameters */
357 gen_next_creds(cli
, &clnt_creds
);
359 init_net_q_sam_deltas(&q
, cli
->srv_name_slash
,
360 cli
->clnt_name_slash
+ 2, &clnt_creds
,
361 database_id
, seqnum
);
363 /* Marshall data and send request */
365 if (!net_io_q_sam_deltas("", &q
, &qbuf
, 0) ||
366 !rpc_api_pipe_req(cli
, NET_SAM_DELTAS
, &qbuf
, &rbuf
)) {
367 result
= NT_STATUS_UNSUCCESSFUL
;
371 /* Unmarshall response */
373 if (!net_io_r_sam_deltas("", cli
->sess_key
, &r
, &rbuf
, 0)) {
374 result
= NT_STATUS_UNSUCCESSFUL
;
381 *num_deltas
= r
.num_deltas2
;
382 *hdr_deltas
= r
.hdr_deltas
;
392 /* Logon domain user */
394 NTSTATUS
cli_netlogon_sam_logon(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
395 char *username
, char *password
,
398 prs_struct qbuf
, rbuf
;
401 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
402 DOM_CRED clnt_creds
, dummy_rtn_creds
;
403 extern pstring global_myname
;
405 NET_USER_INFO_3 user
;
406 int validation_level
= 3;
411 /* Initialise parse structures */
413 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
414 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
416 /* Initialise input parameters */
418 gen_next_creds(cli
, &clnt_creds
);
420 q
.validation_level
= validation_level
;
422 memset(&dummy_rtn_creds
, '\0', sizeof(dummy_rtn_creds
));
423 dummy_rtn_creds
.timestamp
.time
= time(NULL
);
425 ctr
.switch_value
= logon_type
;
427 switch (logon_type
) {
428 case INTERACTIVE_LOGON_TYPE
: {
429 unsigned char lm_owf_user_pwd
[16], nt_owf_user_pwd
[16];
431 nt_lm_owf_gen(password
, nt_owf_user_pwd
, lm_owf_user_pwd
);
433 init_id_info1(&ctr
.auth
.id1
, lp_workgroup(),
435 0xdead, 0xbeef, /* LUID? */
436 username
, cli
->clnt_name_slash
,
437 cli
->sess_key
, lm_owf_user_pwd
,
442 case NET_LOGON_TYPE
: {
444 unsigned char local_lm_response
[24];
445 unsigned char local_nt_response
[24];
447 generate_random_buffer(chal
, 8, False
);
449 SMBencrypt(password
, chal
, local_lm_response
);
450 SMBNTencrypt(password
, chal
, local_nt_response
);
452 init_id_info2(&ctr
.auth
.id2
, lp_workgroup(),
454 0xdead, 0xbeef, /* LUID? */
455 username
, cli
->clnt_name_slash
, chal
,
456 local_lm_response
, 24, local_nt_response
, 24);
460 DEBUG(0, ("switch value %d not supported\n",
465 init_sam_info(&q
.sam_id
, cli
->srv_name_slash
, global_myname
,
466 &clnt_creds
, &dummy_rtn_creds
, logon_type
,
469 /* Marshall data and send request */
471 if (!net_io_q_sam_logon("", &q
, &qbuf
, 0) ||
472 !rpc_api_pipe_req(cli
, NET_SAMLOGON
, &qbuf
, &rbuf
)) {
476 /* Unmarshall response */
480 if (!net_io_r_sam_logon("", &r
, &rbuf
, 0)) {
497 * Logon domain user with an 'network' SAM logon
499 * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
502 NTSTATUS
cli_netlogon_sam_network_logon(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
503 const char *username
, const char *domain
, const char *workstation
,
505 DATA_BLOB lm_response
, DATA_BLOB nt_response
,
506 NET_USER_INFO_3
*info3
)
509 prs_struct qbuf
, rbuf
;
512 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
513 DOM_CRED clnt_creds
, dummy_rtn_creds
;
515 extern pstring global_myname
;
516 int validation_level
= 3;
517 char *workstation_name_slash
;
522 workstation_name_slash
= talloc_asprintf(mem_ctx
, "\\\\%s", workstation
);
524 if (!workstation_name_slash
) {
525 DEBUG(0, ("talloc_asprintf failed!\n"));
526 return NT_STATUS_NO_MEMORY
;
529 /* Initialise parse structures */
531 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
532 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
534 /* Initialise input parameters */
536 gen_next_creds(cli
, &clnt_creds
);
538 q
.validation_level
= validation_level
;
540 memset(&dummy_rtn_creds
, '\0', sizeof(dummy_rtn_creds
));
541 dummy_rtn_creds
.timestamp
.time
= time(NULL
);
543 ctr
.switch_value
= NET_LOGON_TYPE
;
545 init_id_info2(&ctr
.auth
.id2
, domain
,
547 0xdead, 0xbeef, /* LUID? */
548 username
, workstation_name_slash
, (const uchar
*)chal
,
549 lm_response
.data
, lm_response
.length
, nt_response
.data
, nt_response
.length
);
551 init_sam_info(&q
.sam_id
, cli
->srv_name_slash
, global_myname
,
552 &clnt_creds
, &dummy_rtn_creds
, NET_LOGON_TYPE
,
555 /* Marshall data and send request */
557 if (!net_io_q_sam_logon("", &q
, &qbuf
, 0) ||
558 !rpc_api_pipe_req(cli
, NET_SAMLOGON
, &qbuf
, &rbuf
)) {
562 /* Unmarshall response */
566 if (!net_io_r_sam_logon("", &r
, &rbuf
, 0)) {
581 /***************************************************************************
582 LSA Server Password Set.
583 ****************************************************************************/
585 NTSTATUS
cli_net_srv_pwset(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
586 char* machine_name
, uint8 hashed_mach_pwd
[16])
590 DOM_CRED new_clnt_cred
;
592 uint16 sec_chan_type
= 2;
596 gen_next_creds( cli
, &new_clnt_cred
);
598 prs_init(&qbuf
, 1024, mem_ctx
, MARSHALL
);
599 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
601 /* create and send a MSRPC command with api NET_SRV_PWSET */
603 mach_acct
= talloc_asprintf(mem_ctx
, "%s$", machine_name
);
606 DEBUG(0,("talloc_asprintf failed!\n"));
607 nt_status
= NT_STATUS_NO_MEMORY
;
611 DEBUG(4,("cli_net_srv_pwset: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
612 cli
->srv_name_slash
, mach_acct
, sec_chan_type
, machine_name
,
613 credstr(new_clnt_cred
.challenge
.data
), new_clnt_cred
.timestamp
.time
));
615 /* store the parameters */
616 init_q_srv_pwset(&q_s
, cli
->srv_name_slash
, cli
->sess_key
,
617 mach_acct
, sec_chan_type
, machine_name
,
618 &new_clnt_cred
, (char *)hashed_mach_pwd
);
620 /* turn parameters into data stream */
621 if(!net_io_q_srv_pwset("", &q_s
, &qbuf
, 0)) {
622 DEBUG(0,("cli_net_srv_pwset: Error : failed to marshall NET_Q_SRV_PWSET struct.\n"));
623 nt_status
= NT_STATUS_UNSUCCESSFUL
;
627 /* send the data on \PIPE\ */
628 if (rpc_api_pipe_req(cli
, NET_SRVPWSET
, &qbuf
, &rbuf
))
632 if (!net_io_r_srv_pwset("", &r_s
, &rbuf
, 0)) {
633 nt_status
= NT_STATUS_UNSUCCESSFUL
;
637 nt_status
= r_s
.status
;
639 if (!NT_STATUS_IS_OK(r_s
.status
))
641 /* report error code */
642 DEBUG(0,("cli_net_srv_pwset: %s\n", nt_errstr(nt_status
)));
646 /* Update the credentials. */
647 if (!clnt_deal_with_creds(cli
->sess_key
, &(cli
->clnt_cred
), &(r_s
.srv_cred
)))
650 * Server replied with bad credential. Fail.
652 DEBUG(0,("cli_net_srv_pwset: server %s replied with bad credential (bad machine \
653 password ?).\n", cli
->desthost
));
654 nt_status
= NT_STATUS_UNSUCCESSFUL
;