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 /* Opens a SMB connection to the netlogon pipe */
30 struct cli_state
*cli_netlogon_initialise(struct cli_state
*cli
,
32 struct ntuser_creds
*creds
)
34 return cli_pipe_initialise(cli
, system_name
, PIPE_NETLOGON
, creds
);
37 /* LSA Request Challenge. Sends our challenge to server, then gets
38 server response. These are used to generate the credentials. */
40 NTSTATUS
new_cli_net_req_chal(struct cli_state
*cli
, DOM_CHAL
*clnt_chal
,
43 prs_struct qbuf
, rbuf
;
46 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
47 extern pstring global_myname
;
49 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, cli
->mem_ctx
, MARSHALL
);
50 prs_init(&rbuf
, 0, cli
->mem_ctx
, UNMARSHALL
);
52 /* create and send a MSRPC command with api NET_REQCHAL */
54 DEBUG(4,("new_cli_net_req_chal: LSA Request Challenge from %s to %s: %s\n",
55 cli
->desthost
, global_myname
, credstr(clnt_chal
->data
)));
57 /* store the parameters */
58 init_q_req_chal(&q
, cli
->srv_name_slash
, global_myname
, clnt_chal
);
60 /* Marshall data and send request */
62 if (!net_io_q_req_chal("", &q
, &qbuf
, 0) ||
63 !rpc_api_pipe_req(cli
, NET_REQCHAL
, &qbuf
, &rbuf
)) {
67 /* Unmarhall response */
69 if (!net_io_r_req_chal("", &r
, &rbuf
, 0)) {
77 if (NT_STATUS_IS_OK(result
)) {
78 memcpy(srv_chal
, r
.srv_chal
.data
, sizeof(srv_chal
->data
));
88 /****************************************************************************
91 Send the client credential, receive back a server credential.
92 Ensure that the server credential returned matches the session key
93 encrypt of the server challenge originally received. JRA.
94 ****************************************************************************/
96 NTSTATUS
new_cli_net_auth2(struct cli_state
*cli
,
98 uint32 neg_flags
, DOM_CHAL
*srv_chal
)
100 prs_struct qbuf
, rbuf
;
103 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
104 extern pstring global_myname
;
106 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, cli
->mem_ctx
, MARSHALL
);
107 prs_init(&rbuf
, 0, cli
->mem_ctx
, UNMARSHALL
);
109 /* create and send a MSRPC command with api NET_AUTH2 */
111 DEBUG(4,("new_cli_net_auth2: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
112 cli
->srv_name_slash
, cli
->mach_acct
, sec_chan
, global_myname
,
113 credstr(cli
->clnt_cred
.challenge
.data
), neg_flags
));
115 /* store the parameters */
116 init_q_auth_2(&q
, cli
->srv_name_slash
, cli
->mach_acct
,
117 sec_chan
, global_myname
, &cli
->clnt_cred
.challenge
,
120 /* turn parameters into data stream */
122 if (!net_io_q_auth_2("", &q
, &qbuf
, 0) ||
123 !rpc_api_pipe_req(cli
, NET_AUTH2
, &qbuf
, &rbuf
)) {
127 /* Unmarshall response */
129 if (!net_io_r_auth_2("", &r
, &rbuf
, 0)) {
135 if (NT_STATUS_IS_OK(result
)) {
139 * Check the returned value using the initial
140 * server received challenge.
144 if (cred_assert( &r
.srv_chal
, cli
->sess_key
, srv_chal
,
148 * Server replied with bad credential. Fail.
150 DEBUG(0,("new_cli_net_auth2: server %s replied with bad credential (bad machine \
151 password ?).\n", cli
->desthost
));
152 result
= NT_STATUS_ACCESS_DENIED
;
164 /* Initialize domain session credentials */
166 NTSTATUS
new_cli_nt_setup_creds(struct cli_state
*cli
,
168 const unsigned char mach_pwd
[16])
175 /******************* Request Challenge ********************/
177 generate_random_buffer(clnt_chal
.data
, 8, False
);
179 /* send a client challenge; receive a server challenge */
180 result
= new_cli_net_req_chal(cli
, &clnt_chal
, &srv_chal
);
182 if (!NT_STATUS_IS_OK(result
)) {
183 DEBUG(0,("new_cli_nt_setup_creds: request challenge failed\n"));
187 /**************** Long-term Session key **************/
189 /* calculate the session key */
190 cred_session_key(&clnt_chal
, &srv_chal
, mach_pwd
,
192 memset((char *)cli
->sess_key
+8, '\0', 8);
194 /******************* Authenticate 2 ********************/
196 /* calculate auth-2 credentials */
198 cred_create(cli
->sess_key
, &clnt_chal
, zerotime
,
199 &cli
->clnt_cred
.challenge
);
202 * Send client auth-2 challenge.
203 * Receive an auth-2 challenge response and check it.
206 result
= new_cli_net_auth2(cli
, sec_chan
, 0x000001ff,
208 if (!NT_STATUS_IS_OK(result
)) {
209 DEBUG(0,("new_cli_nt_setup_creds: auth2 challenge failed %s\n",
210 get_nt_error_msg(result
)));
216 /* Logon Control 2 */
218 NTSTATUS
cli_netlogon_logon_ctrl2(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
221 prs_struct qbuf
, rbuf
;
224 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
229 /* Initialise parse structures */
231 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
232 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
234 /* Initialise input parameters */
236 init_net_q_logon_ctrl2(&q
, cli
->srv_name_slash
, query_level
);
238 /* Marshall data and send request */
240 if (!net_io_q_logon_ctrl2("", &q
, &qbuf
, 0) ||
241 !rpc_api_pipe_req(cli
, NET_LOGON_CTRL2
, &qbuf
, &rbuf
)) {
242 result
= NT_STATUS_UNSUCCESSFUL
;
246 /* Unmarshall response */
248 if (!net_io_r_logon_ctrl2("", &r
, &rbuf
, 0)) {
249 result
= NT_STATUS_UNSUCCESSFUL
;
262 /****************************************************************************
263 Generate the next creds to use. Yuck - this is a cut&paste from another
264 file. They should be combined at some stage. )-:
265 ****************************************************************************/
267 static void gen_next_creds( struct cli_state
*cli
, DOM_CRED
*new_clnt_cred
)
270 * Create the new client credentials.
273 cli
->clnt_cred
.timestamp
.time
= time(NULL
);
275 memcpy(new_clnt_cred
, &cli
->clnt_cred
, sizeof(*new_clnt_cred
));
277 /* Calculate the new credentials. */
278 cred_create(cli
->sess_key
, &(cli
->clnt_cred
.challenge
),
279 new_clnt_cred
->timestamp
, &(new_clnt_cred
->challenge
));
283 /* Sam synchronisation */
285 NTSTATUS
cli_netlogon_sam_sync(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
, DOM_CRED
*ret_creds
,
286 uint32 database_id
, uint32
*num_deltas
,
287 SAM_DELTA_HDR
**hdr_deltas
,
288 SAM_DELTA_CTR
**deltas
)
290 prs_struct qbuf
, rbuf
;
293 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
299 /* Initialise parse structures */
301 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
302 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
304 /* Initialise input parameters */
306 gen_next_creds(cli
, &clnt_creds
);
308 init_net_q_sam_sync(&q
, cli
->srv_name_slash
, cli
->clnt_name_slash
+ 2,
309 &clnt_creds
, ret_creds
, database_id
);
311 /* Marshall data and send request */
313 if (!net_io_q_sam_sync("", &q
, &qbuf
, 0) ||
314 !rpc_api_pipe_req(cli
, NET_SAM_SYNC
, &qbuf
, &rbuf
)) {
315 result
= NT_STATUS_UNSUCCESSFUL
;
319 /* Unmarshall response */
321 if (!net_io_r_sam_sync("", cli
->sess_key
, &r
, &rbuf
, 0)) {
322 result
= NT_STATUS_UNSUCCESSFUL
;
329 *num_deltas
= r
.num_deltas2
;
330 *hdr_deltas
= r
.hdr_deltas
;
333 memcpy(ret_creds
, &r
.srv_creds
, sizeof(*ret_creds
));
342 /* Sam synchronisation */
344 NTSTATUS
cli_netlogon_sam_deltas(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
345 uint32 database_id
, UINT64_S seqnum
,
347 SAM_DELTA_HDR
**hdr_deltas
,
348 SAM_DELTA_CTR
**deltas
)
350 prs_struct qbuf
, rbuf
;
353 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
359 /* Initialise parse structures */
361 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
362 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
364 /* Initialise input parameters */
366 gen_next_creds(cli
, &clnt_creds
);
368 init_net_q_sam_deltas(&q
, cli
->srv_name_slash
,
369 cli
->clnt_name_slash
+ 2, &clnt_creds
,
370 database_id
, seqnum
);
372 /* Marshall data and send request */
374 if (!net_io_q_sam_deltas("", &q
, &qbuf
, 0) ||
375 !rpc_api_pipe_req(cli
, NET_SAM_DELTAS
, &qbuf
, &rbuf
)) {
376 result
= NT_STATUS_UNSUCCESSFUL
;
380 /* Unmarshall response */
382 if (!net_io_r_sam_deltas("", cli
->sess_key
, &r
, &rbuf
, 0)) {
383 result
= NT_STATUS_UNSUCCESSFUL
;
390 *num_deltas
= r
.num_deltas2
;
391 *hdr_deltas
= r
.hdr_deltas
;
401 /* Logon domain user */
403 NTSTATUS
cli_netlogon_sam_logon(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
404 const char *unix_username
, const char *unix_password
,
407 prs_struct qbuf
, rbuf
;
410 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
411 DOM_CRED clnt_creds
, dummy_rtn_creds
;
412 extern pstring global_myname
;
414 NET_USER_INFO_3 user
;
415 int validation_level
= 3;
416 fstring dos_username
, dos_password
;
421 fstrcpy(dos_username
, unix_username
);
422 unix_to_dos(dos_username
);
423 fstrcpy(dos_password
, unix_password
);
424 unix_to_dos(dos_password
);
426 /* Initialise parse structures */
428 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
429 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
431 /* Initialise input parameters */
433 gen_next_creds(cli
, &clnt_creds
);
435 q
.validation_level
= validation_level
;
437 memset(&dummy_rtn_creds
, '\0', sizeof(dummy_rtn_creds
));
438 dummy_rtn_creds
.timestamp
.time
= time(NULL
);
440 ctr
.switch_value
= logon_type
;
442 switch (logon_type
) {
443 case INTERACTIVE_LOGON_TYPE
: {
444 unsigned char lm_owf_user_pwd
[16], nt_owf_user_pwd
[16];
446 nt_lm_owf_gen(dos_password
, nt_owf_user_pwd
, lm_owf_user_pwd
);
448 init_id_info1(&ctr
.auth
.id1
, lp_workgroup(),
450 0xdead, 0xbeef, /* LUID? */
451 dos_username
, cli
->clnt_name_slash
,
452 (char *)cli
->sess_key
, lm_owf_user_pwd
,
457 case NET_LOGON_TYPE
: {
459 unsigned char local_lm_response
[24];
460 unsigned char local_nt_response
[24];
462 generate_random_buffer(chal
, 8, False
);
464 SMBencrypt((const uchar
*)dos_password
, chal
, local_lm_response
);
465 SMBNTencrypt((const uchar
*)dos_password
, chal
, local_nt_response
);
467 init_id_info2(&ctr
.auth
.id2
, lp_workgroup(),
469 0xdead, 0xbeef, /* LUID? */
470 dos_username
, cli
->clnt_name_slash
, chal
,
471 local_lm_response
, 24, local_nt_response
, 24);
475 DEBUG(0, ("switch value %d not supported\n",
480 init_sam_info(&q
.sam_id
, cli
->srv_name_slash
, global_myname
,
481 &clnt_creds
, &dummy_rtn_creds
, logon_type
,
484 /* Marshall data and send request */
486 if (!net_io_q_sam_logon("", &q
, &qbuf
, 0) ||
487 !rpc_api_pipe_req(cli
, NET_SAMLOGON
, &qbuf
, &rbuf
)) {
491 /* Unmarshall response */
495 if (!net_io_r_sam_logon("", &r
, &rbuf
, 0)) {
511 * Logon domain user with an 'network' SAM logon
513 * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
516 NTSTATUS
cli_netlogon_sam_network_logon(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
517 const char *unix_username
, const char *unix_domain
, const char *workstation
,
519 DATA_BLOB lm_response
, DATA_BLOB nt_response
,
520 NET_USER_INFO_3
*info3
)
523 prs_struct qbuf
, rbuf
;
526 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
527 DOM_CRED clnt_creds
, dummy_rtn_creds
;
529 extern pstring global_myname
;
530 int validation_level
= 3;
531 char *workstation_name_slash
;
532 fstring dos_username
, dos_domain
;
537 fstrcpy(dos_username
, unix_username
);
538 unix_to_dos(dos_username
);
539 fstrcpy(dos_domain
, unix_domain
);
540 unix_to_dos(dos_domain
);
542 workstation_name_slash
= talloc_asprintf(mem_ctx
, "\\\\%s", workstation
);
544 if (!workstation_name_slash
) {
545 DEBUG(0, ("talloc_asprintf failed!\n"));
546 return NT_STATUS_NO_MEMORY
;
549 /* Initialise parse structures */
551 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
552 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
554 /* Initialise input parameters */
556 gen_next_creds(cli
, &clnt_creds
);
558 q
.validation_level
= validation_level
;
560 memset(&dummy_rtn_creds
, '\0', sizeof(dummy_rtn_creds
));
561 dummy_rtn_creds
.timestamp
.time
= time(NULL
);
563 ctr
.switch_value
= NET_LOGON_TYPE
;
565 init_id_info2(&ctr
.auth
.id2
, dos_domain
,
567 0xdead, 0xbeef, /* LUID? */
568 dos_username
, workstation_name_slash
, (const uchar
*)chal
,
569 lm_response
.data
, lm_response
.length
, nt_response
.data
, nt_response
.length
);
571 init_sam_info(&q
.sam_id
, cli
->srv_name_slash
, global_myname
,
572 &clnt_creds
, &dummy_rtn_creds
, NET_LOGON_TYPE
,
575 /* Marshall data and send request */
577 if (!net_io_q_sam_logon("", &q
, &qbuf
, 0) ||
578 !rpc_api_pipe_req(cli
, NET_SAMLOGON
, &qbuf
, &rbuf
)) {
582 /* Unmarshall response */
586 if (!net_io_r_sam_logon("", &r
, &rbuf
, 0)) {
604 * Still using old implementation in rpc_client/cli_netlogon.c
607 /***************************************************************************
608 LSA Server Password Set.
609 ****************************************************************************/
611 NTSTATUS
cli_net_srv_pwset(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
612 char* machine_name
, uint8 hashed_mach_pwd
[16])
616 DOM_CRED new_clnt_cred
;
618 uint16 sec_chan_type
= 2;
622 gen_next_creds( cli
, &new_clnt_cred
);
624 prs_init(&qbuf
, 1024, mem_ctx
, MARSHALL
);
625 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
627 /* create and send a MSRPC command with api NET_SRV_PWSET */
629 mach_acct
= talloc_asprintf(mem_ctx
, "%s$", machine_name
);
632 DEBUG(0,("talloc_asprintf failed!\n"));
633 nt_status
= NT_STATUS_NO_MEMORY
;
637 DEBUG(4,("cli_net_srv_pwset: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
638 cli
->srv_name_slash
, mach_acct
, sec_chan_type
, machine_name
,
639 credstr(new_clnt_cred
.challenge
.data
), new_clnt_cred
.timestamp
.time
));
641 /* store the parameters */
642 init_q_srv_pwset(&q_s
, cli
->srv_name_slash
, cli
->sess_key
,
643 mach_acct
, sec_chan_type
, machine_name
,
644 &new_clnt_cred
, (char *)hashed_mach_pwd
);
646 /* turn parameters into data stream */
647 if(!net_io_q_srv_pwset("", &q_s
, &qbuf
, 0)) {
648 DEBUG(0,("cli_net_srv_pwset: Error : failed to marshall NET_Q_SRV_PWSET struct.\n"));
649 nt_status
= NT_STATUS_UNSUCCESSFUL
;
653 /* send the data on \PIPE\ */
654 if (rpc_api_pipe_req(cli
, NET_SRVPWSET
, &qbuf
, &rbuf
))
658 if (!net_io_r_srv_pwset("", &r_s
, &rbuf
, 0)) {
659 nt_status
= NT_STATUS_UNSUCCESSFUL
;
663 nt_status
= r_s
.status
;
665 if (!NT_STATUS_IS_OK(r_s
.status
))
667 /* report error code */
668 DEBUG(0,("cli_net_srv_pwset: %s\n", nt_errstr(nt_status
)));
672 /* Update the credentials. */
673 if (!clnt_deal_with_creds(cli
->sess_key
, &(cli
->clnt_cred
), &(r_s
.srv_cred
)))
676 * Server replied with bad credential. Fail.
678 DEBUG(0,("cli_net_srv_pwset: server %s replied with bad credential (bad machine \
679 password ?).\n", cli
->desthost
));
680 nt_status
= NT_STATUS_UNSUCCESSFUL
;