fixes for unpakcaged files and removeing *.old (from Eirik Thorsnes)
[Samba.git] / source / libsmb / cli_netlogon.c
blobf6763016aaf90afa1bf7f39a1c1753507ce0a913
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 /* Opens a SMB connection to the netlogon pipe */
30 struct cli_state *cli_netlogon_initialise(struct cli_state *cli,
31 char *system_name,
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,
41 DOM_CHAL *srv_chal)
43 prs_struct qbuf, rbuf;
44 NET_Q_REQ_CHAL q;
45 NET_R_REQ_CHAL r;
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)) {
64 goto done;
67 /* Unmarhall response */
69 if (!net_io_r_req_chal("", &r, &rbuf, 0)) {
70 goto done;
73 result = r.status;
75 /* Return result */
77 if (NT_STATUS_IS_OK(result)) {
78 memcpy(srv_chal, r.srv_chal.data, sizeof(srv_chal->data));
81 done:
82 prs_mem_free(&qbuf);
83 prs_mem_free(&rbuf);
85 return result;
88 /****************************************************************************
89 LSA Authenticate 2
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,
97 uint16 sec_chan,
98 uint32 neg_flags, DOM_CHAL *srv_chal)
100 prs_struct qbuf, rbuf;
101 NET_Q_AUTH_2 q;
102 NET_R_AUTH_2 r;
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,
118 neg_flags);
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)) {
124 goto done;
127 /* Unmarshall response */
129 if (!net_io_r_auth_2("", &r, &rbuf, 0)) {
130 goto done;
133 result = r.status;
135 if (NT_STATUS_IS_OK(result)) {
136 UTIME zerotime;
139 * Check the returned value using the initial
140 * server received challenge.
143 zerotime.time = 0;
144 if (cred_assert( &r.srv_chal, cli->sess_key, srv_chal,
145 zerotime) == 0) {
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;
153 goto done;
157 done:
158 prs_mem_free(&qbuf);
159 prs_mem_free(&rbuf);
161 return result;
164 /* Initialize domain session credentials */
166 NTSTATUS new_cli_nt_setup_creds(struct cli_state *cli,
167 uint16 sec_chan,
168 const unsigned char mach_pwd[16])
170 DOM_CHAL clnt_chal;
171 DOM_CHAL srv_chal;
172 UTIME zerotime;
173 NTSTATUS result;
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"));
184 return result;
187 /**************** Long-term Session key **************/
189 /* calculate the session key */
190 cred_session_key(&clnt_chal, &srv_chal, mach_pwd,
191 cli->sess_key);
192 memset((char *)cli->sess_key+8, '\0', 8);
194 /******************* Authenticate 2 ********************/
196 /* calculate auth-2 credentials */
197 zerotime.time = 0;
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,
207 &srv_chal);
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)));
213 return result;
216 /* Logon Control 2 */
218 NTSTATUS cli_netlogon_logon_ctrl2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
219 uint32 query_level)
221 prs_struct qbuf, rbuf;
222 NET_Q_LOGON_CTRL2 q;
223 NET_R_LOGON_CTRL2 r;
224 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
226 ZERO_STRUCT(q);
227 ZERO_STRUCT(r);
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;
243 goto done;
246 /* Unmarshall response */
248 if (!net_io_r_logon_ctrl2("", &r, &rbuf, 0)) {
249 result = NT_STATUS_UNSUCCESSFUL;
250 goto done;
253 result = r.status;
255 done:
256 prs_mem_free(&qbuf);
257 prs_mem_free(&rbuf);
259 return result;
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;
291 NET_Q_SAM_SYNC q;
292 NET_R_SAM_SYNC r;
293 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
294 DOM_CRED clnt_creds;
296 ZERO_STRUCT(q);
297 ZERO_STRUCT(r);
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;
316 goto done;
319 /* Unmarshall response */
321 if (!net_io_r_sam_sync("", cli->sess_key, &r, &rbuf, 0)) {
322 result = NT_STATUS_UNSUCCESSFUL;
323 goto done;
326 /* Return results */
328 result = r.status;
329 *num_deltas = r.num_deltas2;
330 *hdr_deltas = r.hdr_deltas;
331 *deltas = r.deltas;
333 memcpy(ret_creds, &r.srv_creds, sizeof(*ret_creds));
335 done:
336 prs_mem_free(&qbuf);
337 prs_mem_free(&rbuf);
339 return result;
342 /* Sam synchronisation */
344 NTSTATUS cli_netlogon_sam_deltas(struct cli_state *cli, TALLOC_CTX *mem_ctx,
345 uint32 database_id, UINT64_S seqnum,
346 uint32 *num_deltas,
347 SAM_DELTA_HDR **hdr_deltas,
348 SAM_DELTA_CTR **deltas)
350 prs_struct qbuf, rbuf;
351 NET_Q_SAM_DELTAS q;
352 NET_R_SAM_DELTAS r;
353 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
354 DOM_CRED clnt_creds;
356 ZERO_STRUCT(q);
357 ZERO_STRUCT(r);
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;
377 goto done;
380 /* Unmarshall response */
382 if (!net_io_r_sam_deltas("", cli->sess_key, &r, &rbuf, 0)) {
383 result = NT_STATUS_UNSUCCESSFUL;
384 goto done;
387 /* Return results */
389 result = r.status;
390 *num_deltas = r.num_deltas2;
391 *hdr_deltas = r.hdr_deltas;
392 *deltas = r.deltas;
394 done:
395 prs_mem_free(&qbuf);
396 prs_mem_free(&rbuf);
398 return result;
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,
405 int logon_type)
407 prs_struct qbuf, rbuf;
408 NET_Q_SAM_LOGON q;
409 NET_R_SAM_LOGON r;
410 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
411 DOM_CRED clnt_creds, dummy_rtn_creds;
412 extern pstring global_myname;
413 NET_ID_INFO_CTR ctr;
414 NET_USER_INFO_3 user;
415 int validation_level = 3;
416 fstring dos_username, dos_password;
418 ZERO_STRUCT(q);
419 ZERO_STRUCT(r);
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(),
449 0, /* param_ctrl */
450 0xdead, 0xbeef, /* LUID? */
451 dos_username, cli->clnt_name_slash,
452 (char *)cli->sess_key, lm_owf_user_pwd,
453 nt_owf_user_pwd);
455 break;
457 case NET_LOGON_TYPE: {
458 uint8 chal[8];
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(),
468 0, /* param_ctrl */
469 0xdead, 0xbeef, /* LUID? */
470 dos_username, cli->clnt_name_slash, chal,
471 local_lm_response, 24, local_nt_response, 24);
472 break;
474 default:
475 DEBUG(0, ("switch value %d not supported\n",
476 ctr.switch_value));
477 goto done;
480 init_sam_info(&q.sam_id, cli->srv_name_slash, global_myname,
481 &clnt_creds, &dummy_rtn_creds, logon_type,
482 &ctr);
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)) {
488 goto done;
491 /* Unmarshall response */
493 r.user = &user;
495 if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
496 goto done;
499 /* Return results */
501 result = r.status;
503 done:
504 prs_mem_free(&qbuf);
505 prs_mem_free(&rbuf);
507 return result;
510 /**
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,
518 const uint8 chal[8],
519 DATA_BLOB lm_response, DATA_BLOB nt_response,
520 NET_USER_INFO_3 *info3)
523 prs_struct qbuf, rbuf;
524 NET_Q_SAM_LOGON q;
525 NET_R_SAM_LOGON r;
526 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
527 DOM_CRED clnt_creds, dummy_rtn_creds;
528 NET_ID_INFO_CTR ctr;
529 extern pstring global_myname;
530 int validation_level = 3;
531 char *workstation_name_slash;
532 fstring dos_username, dos_domain;
534 ZERO_STRUCT(q);
535 ZERO_STRUCT(r);
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,
566 0, /* param_ctrl */
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,
573 &ctr);
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)) {
579 goto done;
582 /* Unmarshall response */
584 r.user = info3;
586 if (!net_io_r_sam_logon("", &r, &rbuf, 0)) {
587 goto done;
590 /* Return results */
592 result = r.status;
594 done:
595 prs_mem_free(&qbuf);
596 prs_mem_free(&rbuf);
598 return result;
601 #if 0 /* JERRY */
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])
614 prs_struct rbuf;
615 prs_struct qbuf;
616 DOM_CRED new_clnt_cred;
617 NET_Q_SRV_PWSET q_s;
618 uint16 sec_chan_type = 2;
619 NTSTATUS nt_status;
620 char *mach_acct;
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);
631 if (!mach_acct) {
632 DEBUG(0,("talloc_asprintf failed!\n"));
633 nt_status = NT_STATUS_NO_MEMORY;
634 goto done;
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;
650 goto done;
653 /* send the data on \PIPE\ */
654 if (rpc_api_pipe_req(cli, NET_SRVPWSET, &qbuf, &rbuf))
656 NET_R_SRV_PWSET r_s;
658 if (!net_io_r_srv_pwset("", &r_s, &rbuf, 0)) {
659 nt_status = NT_STATUS_UNSUCCESSFUL;
660 goto done;
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)));
669 goto done;
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;
684 done:
685 prs_mem_free(&qbuf);
686 prs_mem_free(&rbuf);
688 return nt_status;
691 #endif /* JERRY */