* small formatting fixes
[Samba.git] / source / rpc_client / cli_netlogon.c
blobacc913554283148ebac67bbd32a9d33cda4f6bd3
1 /*
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.
26 #include "includes.h"
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,
32 DOM_CHAL *srv_chal)
34 prs_struct qbuf, rbuf;
35 NET_Q_REQ_CHAL q;
36 NET_R_REQ_CHAL r;
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)) {
55 goto done;
58 /* Unmarhall response */
60 if (!net_io_r_req_chal("", &r, &rbuf, 0)) {
61 goto done;
64 result = r.status;
66 /* Return result */
68 if (NT_STATUS_IS_OK(result)) {
69 memcpy(srv_chal, r.srv_chal.data, sizeof(srv_chal->data));
72 done:
73 prs_mem_free(&qbuf);
74 prs_mem_free(&rbuf);
76 return result;
79 /****************************************************************************
80 LSA Authenticate 2
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,
88 uint16 sec_chan,
89 uint32 neg_flags, DOM_CHAL *srv_chal)
91 prs_struct qbuf, rbuf;
92 NET_Q_AUTH_2 q;
93 NET_R_AUTH_2 r;
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,
109 neg_flags);
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)) {
115 goto done;
118 /* Unmarshall response */
120 if (!net_io_r_auth_2("", &r, &rbuf, 0)) {
121 goto done;
124 result = r.status;
126 if (NT_STATUS_IS_OK(result)) {
127 UTIME zerotime;
130 * Check the returned value using the initial
131 * server received challenge.
134 zerotime.time = 0;
135 if (cred_assert( &r.srv_chal, cli->sess_key, srv_chal,
136 zerotime) == 0) {
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;
144 goto done;
148 done:
149 prs_mem_free(&qbuf);
150 prs_mem_free(&rbuf);
152 return result;
155 /* Initialize domain session credentials */
157 NTSTATUS cli_nt_setup_creds(struct cli_state *cli,
158 uint16 sec_chan,
159 const unsigned char mach_pwd[16])
161 DOM_CHAL clnt_chal;
162 DOM_CHAL srv_chal;
163 UTIME zerotime;
164 NTSTATUS result;
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"));
175 return result;
178 /**************** Long-term Session key **************/
180 /* calculate the session key */
181 cred_session_key(&clnt_chal, &srv_chal, mach_pwd,
182 cli->sess_key);
183 memset((char *)cli->sess_key+8, '\0', 8);
185 /******************* Authenticate 2 ********************/
187 /* calculate auth-2 credentials */
188 zerotime.time = 0;
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",
201 nt_errstr(result)));
204 return result;
207 /* Logon Control 2 */
209 NTSTATUS cli_netlogon_logon_ctrl2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
210 uint32 query_level)
212 prs_struct qbuf, rbuf;
213 NET_Q_LOGON_CTRL2 q;
214 NET_R_LOGON_CTRL2 r;
215 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
217 ZERO_STRUCT(q);
218 ZERO_STRUCT(r);
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;
234 goto done;
237 /* Unmarshall response */
239 if (!net_io_r_logon_ctrl2("", &r, &rbuf, 0)) {
240 result = NT_STATUS_UNSUCCESSFUL;
241 goto done;
244 result = r.status;
246 done:
247 prs_mem_free(&qbuf);
248 prs_mem_free(&rbuf);
250 return result;
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;
282 NET_Q_SAM_SYNC q;
283 NET_R_SAM_SYNC r;
284 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
285 DOM_CRED clnt_creds;
287 ZERO_STRUCT(q);
288 ZERO_STRUCT(r);
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;
307 goto done;
310 /* Unmarshall response */
312 if (!net_io_r_sam_sync("", cli->sess_key, &r, &rbuf, 0)) {
313 result = NT_STATUS_UNSUCCESSFUL;
314 goto done;
317 /* Return results */
319 result = r.status;
320 *num_deltas = r.num_deltas2;
321 *hdr_deltas = r.hdr_deltas;
322 *deltas = r.deltas;
324 memcpy(ret_creds, &r.srv_creds, sizeof(*ret_creds));
326 done:
327 prs_mem_free(&qbuf);
328 prs_mem_free(&rbuf);
330 return result;
333 /* Sam synchronisation */
335 NTSTATUS cli_netlogon_sam_deltas(struct cli_state *cli, TALLOC_CTX *mem_ctx,
336 uint32 database_id, UINT64_S seqnum,
337 uint32 *num_deltas,
338 SAM_DELTA_HDR **hdr_deltas,
339 SAM_DELTA_CTR **deltas)
341 prs_struct qbuf, rbuf;
342 NET_Q_SAM_DELTAS q;
343 NET_R_SAM_DELTAS r;
344 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
345 DOM_CRED clnt_creds;
347 ZERO_STRUCT(q);
348 ZERO_STRUCT(r);
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;
368 goto done;
371 /* Unmarshall response */
373 if (!net_io_r_sam_deltas("", cli->sess_key, &r, &rbuf, 0)) {
374 result = NT_STATUS_UNSUCCESSFUL;
375 goto done;
378 /* Return results */
380 result = r.status;
381 *num_deltas = r.num_deltas2;
382 *hdr_deltas = r.hdr_deltas;
383 *deltas = r.deltas;
385 done:
386 prs_mem_free(&qbuf);
387 prs_mem_free(&rbuf);
389 return result;
392 /* Logon domain user */
394 NTSTATUS cli_netlogon_sam_logon(struct cli_state *cli, TALLOC_CTX *mem_ctx,
395 char *username, char *password,
396 int logon_type)
398 prs_struct qbuf, rbuf;
399 NET_Q_SAM_LOGON q;
400 NET_R_SAM_LOGON r;
401 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
402 DOM_CRED clnt_creds, dummy_rtn_creds;
403 extern pstring global_myname;
404 NET_ID_INFO_CTR ctr;
405 NET_USER_INFO_3 user;
406 int validation_level = 3;
408 ZERO_STRUCT(q);
409 ZERO_STRUCT(r);
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(),
434 0, /* param_ctrl */
435 0xdead, 0xbeef, /* LUID? */
436 username, cli->clnt_name_slash,
437 cli->sess_key, lm_owf_user_pwd,
438 nt_owf_user_pwd);
440 break;
442 case NET_LOGON_TYPE: {
443 uint8 chal[8];
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(),
453 0, /* param_ctrl */
454 0xdead, 0xbeef, /* LUID? */
455 username, cli->clnt_name_slash, chal,
456 local_lm_response, 24, local_nt_response, 24);
457 break;
459 default:
460 DEBUG(0, ("switch value %d not supported\n",
461 ctr.switch_value));
462 goto done;
465 init_sam_info(&q.sam_id, cli->srv_name_slash, global_myname,
466 &clnt_creds, &dummy_rtn_creds, logon_type,
467 &ctr);
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)) {
473 goto done;
476 /* Unmarshall response */
478 r.user = &user;
480 if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
481 goto done;
484 /* Return results */
486 result = r.status;
488 done:
489 prs_mem_free(&qbuf);
490 prs_mem_free(&rbuf);
492 return result;
496 /**
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,
504 const uint8 chal[8],
505 DATA_BLOB lm_response, DATA_BLOB nt_response,
506 NET_USER_INFO_3 *info3)
509 prs_struct qbuf, rbuf;
510 NET_Q_SAM_LOGON q;
511 NET_R_SAM_LOGON r;
512 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
513 DOM_CRED clnt_creds, dummy_rtn_creds;
514 NET_ID_INFO_CTR ctr;
515 extern pstring global_myname;
516 int validation_level = 3;
517 char *workstation_name_slash;
519 ZERO_STRUCT(q);
520 ZERO_STRUCT(r);
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,
546 0, /* param_ctrl */
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,
553 &ctr);
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)) {
559 goto done;
562 /* Unmarshall response */
564 r.user = info3;
566 if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
567 goto done;
570 /* Return results */
572 result = r.status;
574 done:
575 prs_mem_free(&qbuf);
576 prs_mem_free(&rbuf);
578 return result;
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])
588 prs_struct rbuf;
589 prs_struct qbuf;
590 DOM_CRED new_clnt_cred;
591 NET_Q_SRV_PWSET q_s;
592 uint16 sec_chan_type = 2;
593 NTSTATUS nt_status;
594 char *mach_acct;
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);
605 if (!mach_acct) {
606 DEBUG(0,("talloc_asprintf failed!\n"));
607 nt_status = NT_STATUS_NO_MEMORY;
608 goto done;
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;
624 goto done;
627 /* send the data on \PIPE\ */
628 if (rpc_api_pipe_req(cli, NET_SRVPWSET, &qbuf, &rbuf))
630 NET_R_SRV_PWSET r_s;
632 if (!net_io_r_srv_pwset("", &r_s, &rbuf, 0)) {
633 nt_status = NT_STATUS_UNSUCCESSFUL;
634 goto done;
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)));
643 goto done;
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;
658 done:
659 prs_mem_free(&qbuf);
660 prs_mem_free(&rbuf);
662 return nt_status;