2 Unix SMB/Netbios implementation.
4 NT Domain Authentication SMB / MSRPC client
5 Copyright (C) Andrew Tridgell 1994-2000
6 Copyright (C) Luke Kenneth Casson Leighton 1996-2000
7 Copyright (C) Tim Potter 2001
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /* Opens a SMB connection to the netlogon pipe */
28 struct cli_state *cli_netlogon_initialise(struct cli_state *cli,
30 struct ntuser_creds *creds)
32 return cli_pipe_initialise(cli, system_name, PIPE_NETLOGON, creds);
35 /* LSA Request Challenge. Sends our challenge to server, then gets
36 server response. These are used to generate the credentials. */
38 NTSTATUS new_cli_net_req_chal(struct cli_state *cli, DOM_CHAL *clnt_chal,
41 prs_struct qbuf, rbuf;
44 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
45 extern pstring global_myname;
47 prs_init(&qbuf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
48 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
50 /* create and send a MSRPC command with api NET_REQCHAL */
52 DEBUG(4,("cli_net_req_chal: LSA Request Challenge from %s to %s: %s\n",
53 cli->desthost, global_myname, credstr(clnt_chal->data)));
55 /* store the parameters */
56 init_q_req_chal(&q, cli->srv_name_slash, global_myname, clnt_chal);
58 /* Marshall data and send request */
60 if (!net_io_q_req_chal("", &q, &qbuf, 0) ||
61 !rpc_api_pipe_req(cli, NET_REQCHAL, &qbuf, &rbuf)) {
65 /* Unmarhall response */
67 if (!net_io_r_req_chal("", &r, &rbuf, 0)) {
75 if (result == NT_STATUS_OK) {
76 memcpy(srv_chal, r.srv_chal.data, sizeof(srv_chal->data));
86 /****************************************************************************
89 Send the client credential, receive back a server credential.
90 Ensure that the server credential returned matches the session key
91 encrypt of the server challenge originally received. JRA.
92 ****************************************************************************/
94 NTSTATUS new_cli_net_auth2(struct cli_state *cli, uint16 sec_chan,
95 uint32 neg_flags, DOM_CHAL *srv_chal)
97 prs_struct qbuf, rbuf;
100 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
101 extern pstring global_myname;
103 prs_init(&qbuf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
104 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
106 /* create and send a MSRPC command with api NET_AUTH2 */
108 DEBUG(4,("cli_net_auth2: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
109 cli->srv_name_slash, cli->mach_acct, sec_chan, global_myname,
110 credstr(cli->clnt_cred.challenge.data), neg_flags));
112 /* store the parameters */
113 init_q_auth_2(&q, cli->srv_name_slash, cli->mach_acct,
114 sec_chan, global_myname, &cli->clnt_cred.challenge,
117 /* turn parameters into data stream */
119 if (!net_io_q_auth_2("", &q, &qbuf, 0) ||
120 !rpc_api_pipe_req(cli, NET_AUTH2, &qbuf, &rbuf)) {
124 /* Unmarshall response */
126 if (!net_io_r_auth_2("", &r, &rbuf, 0)) {
132 if (result == NT_STATUS_OK) {
136 * Check the returned value using the initial
137 * server received challenge.
141 if (cred_assert( &r.srv_chal, cli->sess_key, srv_chal,
145 * Server replied with bad credential. Fail.
147 DEBUG(0,("cli_net_auth2: server %s replied with bad credential (bad machine \
148 password ?).\n", cli->desthost ));
149 result = NT_STATUS_ACCESS_DENIED;
161 /* Initialize domain session credentials */
163 NTSTATUS new_cli_nt_setup_creds(struct cli_state *cli,
164 unsigned char mach_pwd[16])
171 /******************* Request Challenge ********************/
173 generate_random_buffer(clnt_chal.data, 8, False);
175 /* send a client challenge; receive a server challenge */
176 result = new_cli_net_req_chal(cli, &clnt_chal, &srv_chal);
178 if (result != NT_STATUS_OK) {
179 DEBUG(0,("cli_nt_setup_creds: request challenge failed\n"));
183 /**************** Long-term Session key **************/
185 /* calculate the session key */
186 cred_session_key(&clnt_chal, &srv_chal, (char *)mach_pwd,
188 memset((char *)cli->sess_key+8, '\0', 8);
190 /******************* Authenticate 2 ********************/
192 /* calculate auth-2 credentials */
194 cred_create(cli->sess_key, &clnt_chal, zerotime,
195 &cli->clnt_cred.challenge);
198 * Send client auth-2 challenge.
199 * Receive an auth-2 challenge response and check it.
202 if (!new_cli_net_auth2(cli, (lp_server_role() == ROLE_DOMAIN_MEMBER) ?
203 SEC_CHAN_WKSTA : SEC_CHAN_BDC, 0x000001ff,
205 DEBUG(0,("cli_nt_setup_creds: auth2 challenge failed\n"));
212 /* Logon Control 2 */
214 NTSTATUS cli_netlogon_logon_ctrl2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
217 prs_struct qbuf, rbuf;
220 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
225 /* Initialise parse structures */
227 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
228 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
230 /* Initialise input parameters */
232 init_net_q_logon_ctrl2(&q, cli->srv_name_slash, query_level);
234 /* Marshall data and send request */
236 if (!net_io_q_logon_ctrl2("", &q, &qbuf, 0) ||
237 !rpc_api_pipe_req(cli, NET_LOGON_CTRL2, &qbuf, &rbuf)) {
238 result = NT_STATUS_UNSUCCESSFUL;
242 /* Unmarshall response */
244 if (!net_io_r_logon_ctrl2("", &r, &rbuf, 0)) {
245 result = NT_STATUS_UNSUCCESSFUL;
258 /****************************************************************************
259 Generate the next creds to use. Yuck - this is a cut&paste from another
260 file. They should be combined at some stage. )-:
261 ****************************************************************************/
263 static void gen_next_creds( struct cli_state *cli, DOM_CRED *new_clnt_cred)
266 * Create the new client credentials.
269 cli->clnt_cred.timestamp.time = time(NULL);
271 memcpy(new_clnt_cred, &cli->clnt_cred, sizeof(*new_clnt_cred));
273 /* Calculate the new credentials. */
274 cred_create(cli->sess_key, &(cli->clnt_cred.challenge),
275 new_clnt_cred->timestamp, &(new_clnt_cred->challenge));
279 /* Sam synchronisation */
281 NTSTATUS cli_netlogon_sam_sync(struct cli_state *cli, TALLOC_CTX *mem_ctx,
282 uint32 database_id, uint32 *num_deltas,
283 SAM_DELTA_HDR **hdr_deltas,
284 SAM_DELTA_CTR **deltas)
286 prs_struct qbuf, rbuf;
289 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
296 /* Initialise parse structures */
298 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
299 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
301 /* Initialise input parameters */
303 gen_next_creds(cli, &clnt_creds);
305 init_net_q_sam_sync(&q, cli->srv_name_slash, cli->clnt_name_slash + 2,
306 &clnt_creds, database_id);
308 /* Marshall data and send request */
310 if (!net_io_q_sam_sync("", &q, &qbuf, 0) ||
311 !rpc_api_pipe_req(cli, NET_SAM_SYNC, &qbuf, &rbuf)) {
312 result = NT_STATUS_UNSUCCESSFUL;
316 /* Unmarshall response */
318 if (!net_io_r_sam_sync("", sess_key, &r, &rbuf, 0)) {
319 result = NT_STATUS_UNSUCCESSFUL;
326 *num_deltas = r.num_deltas2;
327 *hdr_deltas = r.hdr_deltas;
337 /* Sam synchronisation */
339 NTSTATUS cli_netlogon_sam_deltas(struct cli_state *cli, TALLOC_CTX *mem_ctx,
340 uint32 database_id, UINT64_S seqnum,
342 SAM_DELTA_HDR **hdr_deltas,
343 SAM_DELTA_CTR **deltas)
345 prs_struct qbuf, rbuf;
348 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
355 /* Initialise parse structures */
357 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
358 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
360 /* Initialise input parameters */
362 gen_next_creds(cli, &clnt_creds);
364 init_net_q_sam_deltas(&q, cli->srv_name_slash,
365 cli->clnt_name_slash + 2, &clnt_creds,
366 database_id, seqnum);
368 /* Marshall data and send request */
370 if (!net_io_q_sam_deltas("", &q, &qbuf, 0) ||
371 !rpc_api_pipe_req(cli, NET_SAM_DELTAS, &qbuf, &rbuf)) {
372 result = NT_STATUS_UNSUCCESSFUL;
376 /* Unmarshall response */
378 if (!net_io_r_sam_deltas("", sess_key, &r, &rbuf, 0)) {
379 result = NT_STATUS_UNSUCCESSFUL;
386 *num_deltas = r.num_deltas2;
387 *hdr_deltas = r.hdr_deltas;