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,("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, uint16 sec_chan,
97 uint32 neg_flags, DOM_CHAL *srv_chal)
99 prs_struct qbuf, rbuf;
102 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
103 extern pstring global_myname;
105 prs_init(&qbuf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
106 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
108 /* create and send a MSRPC command with api NET_AUTH2 */
110 DEBUG(4,("cli_net_auth2: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
111 cli->srv_name_slash, cli->mach_acct, sec_chan, global_myname,
112 credstr(cli->clnt_cred.challenge.data), neg_flags));
114 /* store the parameters */
115 init_q_auth_2(&q, cli->srv_name_slash, cli->mach_acct,
116 sec_chan, global_myname, &cli->clnt_cred.challenge,
119 /* turn parameters into data stream */
121 if (!net_io_q_auth_2("", &q, &qbuf, 0) ||
122 !rpc_api_pipe_req(cli, NET_AUTH2, &qbuf, &rbuf)) {
126 /* Unmarshall response */
128 if (!net_io_r_auth_2("", &r, &rbuf, 0)) {
134 if (NT_STATUS_IS_OK(result)) {
138 * Check the returned value using the initial
139 * server received challenge.
143 if (cred_assert( &r.srv_chal, cli->sess_key, srv_chal,
147 * Server replied with bad credential. Fail.
149 DEBUG(0,("cli_net_auth2: server %s replied with bad credential (bad machine \
150 password ?).\n", cli->desthost ));
151 result = NT_STATUS_ACCESS_DENIED;
163 /* Initialize domain session credentials */
165 NTSTATUS new_cli_nt_setup_creds(struct cli_state *cli,
166 unsigned char mach_pwd[16])
173 /******************* Request Challenge ********************/
175 generate_random_buffer(clnt_chal.data, 8, False);
177 /* send a client challenge; receive a server challenge */
178 result = new_cli_net_req_chal(cli, &clnt_chal, &srv_chal);
180 if (!NT_STATUS_IS_OK(result)) {
181 DEBUG(0,("cli_nt_setup_creds: request challenge failed\n"));
185 /**************** Long-term Session key **************/
187 /* calculate the session key */
188 cred_session_key(&clnt_chal, &srv_chal, (char *)mach_pwd,
190 memset((char *)cli->sess_key+8, '\0', 8);
192 /******************* Authenticate 2 ********************/
194 /* calculate auth-2 credentials */
196 cred_create(cli->sess_key, &clnt_chal, zerotime,
197 &cli->clnt_cred.challenge);
200 * Send client auth-2 challenge.
201 * Receive an auth-2 challenge response and check it.
204 result = new_cli_net_auth2(cli, (lp_server_role() == ROLE_DOMAIN_MEMBER) ?
205 SEC_CHAN_WKSTA : SEC_CHAN_BDC, 0x000001ff,
207 if (!NT_STATUS_IS_OK(result)) {
208 DEBUG(0,("cli_nt_setup_creds: auth2 challenge failed %s\n",
209 get_nt_error_msg(result)));
215 /* Logon Control 2 */
217 NTSTATUS cli_netlogon_logon_ctrl2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
220 prs_struct qbuf, rbuf;
223 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
228 /* Initialise parse structures */
230 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
231 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
233 /* Initialise input parameters */
235 init_net_q_logon_ctrl2(&q, cli->srv_name_slash, query_level);
237 /* Marshall data and send request */
239 if (!net_io_q_logon_ctrl2("", &q, &qbuf, 0) ||
240 !rpc_api_pipe_req(cli, NET_LOGON_CTRL2, &qbuf, &rbuf)) {
241 result = NT_STATUS_UNSUCCESSFUL;
245 /* Unmarshall response */
247 if (!net_io_r_logon_ctrl2("", &r, &rbuf, 0)) {
248 result = NT_STATUS_UNSUCCESSFUL;
261 /****************************************************************************
262 Generate the next creds to use. Yuck - this is a cut&paste from another
263 file. They should be combined at some stage. )-:
264 ****************************************************************************/
266 static void gen_next_creds( struct cli_state *cli, DOM_CRED *new_clnt_cred)
269 * Create the new client credentials.
272 cli->clnt_cred.timestamp.time = time(NULL);
274 memcpy(new_clnt_cred, &cli->clnt_cred, sizeof(*new_clnt_cred));
276 /* Calculate the new credentials. */
277 cred_create(cli->sess_key, &(cli->clnt_cred.challenge),
278 new_clnt_cred->timestamp, &(new_clnt_cred->challenge));
282 /* Sam synchronisation */
284 NTSTATUS cli_netlogon_sam_sync(struct cli_state *cli, TALLOC_CTX *mem_ctx, DOM_CRED *ret_creds,
285 uint32 database_id, uint32 *num_deltas,
286 SAM_DELTA_HDR **hdr_deltas,
287 SAM_DELTA_CTR **deltas)
289 prs_struct qbuf, rbuf;
292 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
298 /* Initialise parse structures */
300 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
301 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
303 /* Initialise input parameters */
305 gen_next_creds(cli, &clnt_creds);
307 init_net_q_sam_sync(&q, cli->srv_name_slash, cli->clnt_name_slash + 2,
308 &clnt_creds, ret_creds, database_id);
310 /* Marshall data and send request */
312 if (!net_io_q_sam_sync("", &q, &qbuf, 0) ||
313 !rpc_api_pipe_req(cli, NET_SAM_SYNC, &qbuf, &rbuf)) {
314 result = NT_STATUS_UNSUCCESSFUL;
318 /* Unmarshall response */
320 if (!net_io_r_sam_sync("", cli->sess_key, &r, &rbuf, 0)) {
321 result = NT_STATUS_UNSUCCESSFUL;
328 *num_deltas = r.num_deltas2;
329 *hdr_deltas = r.hdr_deltas;
332 memcpy(ret_creds, &r.srv_creds, sizeof(*ret_creds));
341 /* Sam synchronisation */
343 NTSTATUS cli_netlogon_sam_deltas(struct cli_state *cli, TALLOC_CTX *mem_ctx,
344 uint32 database_id, UINT64_S seqnum,
346 SAM_DELTA_HDR **hdr_deltas,
347 SAM_DELTA_CTR **deltas)
349 prs_struct qbuf, rbuf;
352 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
358 /* Initialise parse structures */
360 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
361 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
363 /* Initialise input parameters */
365 gen_next_creds(cli, &clnt_creds);
367 init_net_q_sam_deltas(&q, cli->srv_name_slash,
368 cli->clnt_name_slash + 2, &clnt_creds,
369 database_id, seqnum);
371 /* Marshall data and send request */
373 if (!net_io_q_sam_deltas("", &q, &qbuf, 0) ||
374 !rpc_api_pipe_req(cli, NET_SAM_DELTAS, &qbuf, &rbuf)) {
375 result = NT_STATUS_UNSUCCESSFUL;
379 /* Unmarshall response */
381 if (!net_io_r_sam_deltas("", cli->sess_key, &r, &rbuf, 0)) {
382 result = NT_STATUS_UNSUCCESSFUL;
389 *num_deltas = r.num_deltas2;
390 *hdr_deltas = r.hdr_deltas;
400 /* Logon domain user */
402 NTSTATUS cli_netlogon_sam_logon(struct cli_state *cli, TALLOC_CTX *mem_ctx,
403 char *username, char *password,
406 prs_struct qbuf, rbuf;
409 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
410 DOM_CRED clnt_creds, dummy_rtn_creds;
411 extern pstring global_myname;
413 NET_USER_INFO_3 user;
414 int validation_level = 3;
419 /* Initialise parse structures */
421 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
422 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
424 /* Initialise input parameters */
426 gen_next_creds(cli, &clnt_creds);
428 q.validation_level = validation_level;
430 memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
431 dummy_rtn_creds.timestamp.time = time(NULL);
433 ctr.switch_value = logon_type;
435 switch (logon_type) {
436 case INTERACTIVE_LOGON_TYPE: {
437 unsigned char lm_owf_user_pwd[16], nt_owf_user_pwd[16];
439 nt_lm_owf_gen(password, nt_owf_user_pwd, lm_owf_user_pwd);
441 init_id_info1(&ctr.auth.id1, lp_workgroup(),
443 0xdead, 0xbeef, /* LUID? */
444 username, cli->clnt_name_slash,
445 cli->sess_key, lm_owf_user_pwd,
450 case NET_LOGON_TYPE: {
452 unsigned char local_lm_response[24];
453 unsigned char local_nt_response[24];
455 generate_random_buffer(chal, 8, False);
457 SMBencrypt(password, chal, local_lm_response);
458 SMBNTencrypt(password, chal, local_nt_response);
460 init_id_info2(&ctr.auth.id2, lp_workgroup(),
462 0xdead, 0xbeef, /* LUID? */
463 username, cli->clnt_name_slash, chal,
464 local_lm_response, 24, local_nt_response, 24);
468 DEBUG(0, ("switch value %d not supported\n",
473 init_sam_info(&q.sam_id, cli->srv_name_slash, global_myname,
474 &clnt_creds, &dummy_rtn_creds, logon_type,
477 /* Marshall data and send request */
479 if (!net_io_q_sam_logon("", &q, &qbuf, 0) ||
480 !rpc_api_pipe_req(cli, NET_SAMLOGON, &qbuf, &rbuf)) {
484 /* Unmarshall response */
488 if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
505 * Logon domain user with an 'network' SAM logon
507 * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
510 NTSTATUS cli_netlogon_sam_network_logon(struct cli_state *cli, TALLOC_CTX *mem_ctx,
511 const char *username, const char *domain, const char *workstation,
513 DATA_BLOB lm_response, DATA_BLOB nt_response,
514 NET_USER_INFO_3 *info3)
517 prs_struct qbuf, rbuf;
520 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
521 DOM_CRED clnt_creds, dummy_rtn_creds;
523 extern pstring global_myname;
524 int validation_level = 3;
525 char *workstation_name_slash;
530 workstation_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", workstation);
532 if (!workstation_name_slash) {
533 DEBUG(0, ("talloc_asprintf failed!\n"));
534 return NT_STATUS_NO_MEMORY;
537 /* Initialise parse structures */
539 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
540 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
542 /* Initialise input parameters */
544 gen_next_creds(cli, &clnt_creds);
546 q.validation_level = validation_level;
548 memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
549 dummy_rtn_creds.timestamp.time = time(NULL);
551 ctr.switch_value = NET_LOGON_TYPE;
553 init_id_info2(&ctr.auth.id2, domain,
555 0xdead, 0xbeef, /* LUID? */
556 username, workstation_name_slash, (const uchar*)chal,
557 lm_response.data, lm_response.length, nt_response.data, nt_response.length);
559 init_sam_info(&q.sam_id, cli->srv_name_slash, global_myname,
560 &clnt_creds, &dummy_rtn_creds, NET_LOGON_TYPE,
563 /* Marshall data and send request */
565 if (!net_io_q_sam_logon("", &q, &qbuf, 0) ||
566 !rpc_api_pipe_req(cli, NET_SAMLOGON, &qbuf, &rbuf)) {
570 /* Unmarshall response */
574 if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
589 /***************************************************************************
590 LSA Server Password Set.
591 ****************************************************************************/
593 NTSTATUS cli_net_srv_pwset(struct cli_state *cli, TALLOC_CTX *mem_ctx,
594 char* machine_name, uint8 hashed_mach_pwd[16])
598 DOM_CRED new_clnt_cred;
600 uint16 sec_chan_type = 2;
604 gen_next_creds( cli, &new_clnt_cred);
606 prs_init(&qbuf , 1024, mem_ctx, MARSHALL);
607 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
609 /* create and send a MSRPC command with api NET_SRV_PWSET */
611 mach_acct = talloc_asprintf(mem_ctx, "%s$", machine_name);
614 DEBUG(0,("talloc_asprintf failed!\n"));
615 nt_status = NT_STATUS_NO_MEMORY;
619 DEBUG(4,("cli_net_srv_pwset: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
620 cli->srv_name_slash, mach_acct, sec_chan_type, machine_name,
621 credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time));
623 /* store the parameters */
624 init_q_srv_pwset(&q_s, cli->srv_name_slash, cli->sess_key,
625 mach_acct, sec_chan_type, machine_name,
626 &new_clnt_cred, (char *)hashed_mach_pwd);
628 /* turn parameters into data stream */
629 if(!net_io_q_srv_pwset("", &q_s, &qbuf, 0)) {
630 DEBUG(0,("cli_net_srv_pwset: Error : failed to marshall NET_Q_SRV_PWSET struct.\n"));
631 nt_status = NT_STATUS_UNSUCCESSFUL;
635 /* send the data on \PIPE\ */
636 if (rpc_api_pipe_req(cli, NET_SRVPWSET, &qbuf, &rbuf))
640 if (!net_io_r_srv_pwset("", &r_s, &rbuf, 0)) {
641 nt_status = NT_STATUS_UNSUCCESSFUL;
645 nt_status = r_s.status;
647 if (!NT_STATUS_IS_OK(r_s.status))
649 /* report error code */
650 DEBUG(0,("cli_net_srv_pwset: %s\n", get_nt_error_msg(nt_status)));
654 /* Update the credentials. */
655 if (!clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_cred)))
658 * Server replied with bad credential. Fail.
660 DEBUG(0,("cli_net_srv_pwset: server %s replied with bad credential (bad machine \
661 password ?).\n", cli->desthost ));
662 nt_status = NT_STATUS_UNSUCCESSFUL;