2 * Unix SMB/Netbios implementation.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-1997,
6 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
7 * Copyright (C) Paul Ashton 1997.
8 * Copyright (C) Jeremy Allison 1998.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 extern pstring global_myname;
29 extern fstring global_myworkgroup;
31 /****************************************************************************
32 Generate the next creds to use.
33 ****************************************************************************/
35 static void gen_next_creds( struct cli_state *cli, DOM_CRED *new_clnt_cred)
38 * Create the new client credentials.
41 cli->clnt_cred.timestamp.time = time(NULL);
43 memcpy(new_clnt_cred, &cli->clnt_cred, sizeof(*new_clnt_cred));
45 /* Calculate the new credentials. */
46 cred_create(cli->sess_key, &(cli->clnt_cred.challenge),
47 new_clnt_cred->timestamp, &(new_clnt_cred->challenge));
52 /****************************************************************************
53 do a LSA Logon Control2
54 ****************************************************************************/
55 BOOL cli_net_logon_ctrl2(struct cli_state *cli, NTSTATUS status_level)
59 NET_Q_LOGON_CTRL2 q_l;
62 prs_init(&buf , 1024, cli->mem_ctx, MARSHALL);
63 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
65 /* create and send a MSRPC command with api NET_LOGON_CTRL2 */
67 DEBUG(4,("do_net_logon_ctrl2 from %s status level:%x\n",
68 global_myname, status_level));
70 /* store the parameters */
71 init_q_logon_ctrl2(&q_l, cli->srv_name_slash,
74 /* turn parameters into data stream */
75 if(!net_io_q_logon_ctrl2("", &q_l, &buf, 0)) {
76 DEBUG(0,("cli_net_logon_ctrl2: Error : failed to marshall NET_Q_LOGON_CTRL2 struct.\n"));
82 /* send the data on \PIPE\ */
83 if (rpc_api_pipe_req(cli, NET_LOGON_CTRL2, &buf, &rbuf))
85 NET_R_LOGON_CTRL2 r_l;
88 * Unmarshall the return buffer.
90 ok = net_io_r_logon_ctrl2("", &r_l, &rbuf, 0);
92 if (ok && r_l.status != 0)
94 /* report error code */
95 DEBUG(0,("do_net_logon_ctrl2: Error %s\n", get_nt_error_msg(r_l.status)));
96 cli->nt_error = r_l.status;
108 /****************************************************************************
111 Send the client credential, receive back a server credential.
112 Ensure that the server credential returned matches the session key
113 encrypt of the server challenge originally received. JRA.
114 ****************************************************************************/
116 NTSTATUS cli_net_auth2(struct cli_state *cli, uint16 sec_chan,
117 uint32 neg_flags, DOM_CHAL *srv_chal)
123 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
125 prs_init(&buf , 1024, cli->mem_ctx, MARSHALL);
126 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
128 /* create and send a MSRPC command with api NET_AUTH2 */
130 DEBUG(4,("cli_net_auth2: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
131 cli->srv_name_slash, cli->mach_acct, sec_chan, global_myname,
132 credstr(cli->clnt_cred.challenge.data), neg_flags));
134 /* store the parameters */
135 init_q_auth_2(&q_a, cli->srv_name_slash, cli->mach_acct,
136 sec_chan, global_myname, &cli->clnt_cred.challenge, neg_flags);
138 /* turn parameters into data stream */
139 if(!net_io_q_auth_2("", &q_a, &buf, 0)) {
140 DEBUG(0,("cli_net_auth2: Error : failed to marshall NET_Q_AUTH_2 struct.\n"));
146 /* send the data on \PIPE\ */
147 if (rpc_api_pipe_req(cli, NET_AUTH2, &buf, &rbuf))
151 ok = net_io_r_auth_2("", &r_a, &rbuf, 0);
154 if (ok && !NT_STATUS_IS_OK(result))
156 /* report error code */
157 DEBUG(0,("cli_net_auth2: Error %s\n", get_nt_error_msg(result)));
164 * Check the returned value using the initial
165 * server received challenge.
170 if(cred_assert( &r_a.srv_chal, cli->sess_key, srv_chal, zerotime) == 0) {
172 * Server replied with bad credential. Fail.
174 DEBUG(0,("cli_net_auth2: server %s replied with bad credential (bad machine \
175 password ?).\n", cli->desthost ));
182 * Try commenting this out to see if this makes the connect
183 * work for a NT 3.51 PDC. JRA.
186 if (ok && r_a.srv_flgs.neg_flags != q_a.clnt_flgs.neg_flags)
188 /* report different neg_flags */
189 DEBUG(0,("cli_net_auth2: error neg_flags (q,r) differ - (%x,%x)\n",
190 q_a.clnt_flgs.neg_flags, r_a.srv_flgs.neg_flags));
203 /****************************************************************************
204 LSA Request Challenge. Sends our challenge to server, then gets
205 server response. These are used to generate the credentials.
206 ****************************************************************************/
208 BOOL cli_net_req_chal(struct cli_state *cli, DOM_CHAL *clnt_chal, DOM_CHAL *srv_chal)
213 BOOL valid_chal = False;
215 prs_init(&buf , 1024, cli->mem_ctx, MARSHALL);
216 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
218 /* create and send a MSRPC command with api NET_REQCHAL */
220 DEBUG(4,("cli_net_req_chal: LSA Request Challenge from %s to %s: %s\n",
221 cli->desthost, global_myname, credstr(clnt_chal->data)));
223 /* store the parameters */
224 init_q_req_chal(&q_c, cli->srv_name_slash,
225 global_myname, clnt_chal);
227 /* turn parameters into data stream */
228 if(!net_io_q_req_chal("", &q_c, &buf, 0)) {
229 DEBUG(0,("cli_net_req_chal: Error : failed to marshall NET_Q_REQ_CHAL struct.\n"));
235 /* send the data on \PIPE\ */
236 if (rpc_api_pipe_req(cli, NET_REQCHAL, &buf, &rbuf))
241 ok = net_io_r_req_chal("", &r_c, &rbuf, 0);
243 if (ok && !NT_STATUS_IS_OK(r_c.status))
245 /* report error code */
246 DEBUG(0,("cli_net_req_chal: Error %s\n", get_nt_error_msg(r_c.status)));
252 /* ok, at last: we're happy. return the challenge */
253 memcpy(srv_chal, r_c.srv_chal.data, sizeof(srv_chal->data));
263 /***************************************************************************
264 LSA SAM Logon internal - interactive or network. Does level 2 or 3 but always
266 ****************************************************************************/
268 static NTSTATUS cli_net_sam_logon_internal(struct cli_state *cli, NET_ID_INFO_CTR *ctr,
269 NET_USER_INFO_3 *user_info3,
270 uint16 validation_level)
272 DOM_CRED new_clnt_cred;
273 DOM_CRED dummy_rtn_creds;
278 NTSTATUS retval = NT_STATUS_OK;
280 gen_next_creds( cli, &new_clnt_cred);
282 prs_init(&buf , 1024, cli->mem_ctx, MARSHALL);
283 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
285 /* create and send a MSRPC command with api NET_SAMLOGON */
287 DEBUG(4,("cli_net_sam_logon_internal: srv:%s mc:%s clnt %s %x ll: %d\n",
288 cli->srv_name_slash, global_myname,
289 credstr(new_clnt_cred.challenge.data), cli->clnt_cred.timestamp.time,
292 memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
293 dummy_rtn_creds.timestamp.time = time(NULL);
295 /* store the parameters */
296 q_s.validation_level = validation_level;
297 init_sam_info(&q_s.sam_id, cli->srv_name_slash,
298 global_myname, &new_clnt_cred, &dummy_rtn_creds,
299 ctr->switch_value, ctr);
301 /* turn parameters into data stream */
302 if(!net_io_q_sam_logon("", &q_s, &buf, 0)) {
303 DEBUG(0,("cli_net_sam_logon_internal: Error : failed to marshall NET_Q_SAM_LOGON struct.\n"));
304 retval = NT_STATUS_NO_MEMORY;
308 /* send the data on \PIPE\ */
309 if (!rpc_api_pipe_req(cli, NET_SAMLOGON, &buf, &rbuf)) {
310 DEBUG(0,("cli_net_sam_logon_internal: Error rpc_api_pipe_req failed.\n"));
311 retval = NT_STATUS_UNSUCCESSFUL;
315 r_s.user = user_info3;
317 if(!net_io_r_sam_logon("", &r_s, &rbuf, 0)) {
318 DEBUG(0,("cli_net_sam_logon_internal: Error : failed to unmarshal NET_R_SAM_LOGON struct.\n"));
319 retval = NT_STATUS_NO_MEMORY;
326 * Don't treat NT_STATUS_INVALID_INFO_CLASS as an error - we will re-issue
330 if (NT_STATUS_V(retval) == NT_STATUS_V(NT_STATUS_INVALID_INFO_CLASS)) {
334 if (!NT_STATUS_IS_OK(retval)) {
335 /* report error code */
336 DEBUG(0,("cli_net_sam_logon_internal: %s\n", get_nt_error_msg(r_s.status)));
340 /* Update the credentials. */
341 if (!clnt_deal_with_creds(cli->sess_key, &cli->clnt_cred, &r_s.srv_creds)) {
343 * Server replied with bad credential. Fail.
345 DEBUG(0,("cli_net_sam_logon_internal: server %s replied with bad credential (bad machine \
346 password ?).\n", cli->desthost ));
347 retval = NT_STATUS_WRONG_PASSWORD;
350 if (r_s.switch_value != validation_level) {
351 /* report different switch_value */
352 DEBUG(0,("cli_net_sam_logon: switch_value of %x expected %x\n", (unsigned int)validation_level,
353 (unsigned int)r_s.switch_value));
354 retval = NT_STATUS_INVALID_PARAMETER;
365 /***************************************************************************
366 LSA SAM Logon - interactive or network.
367 ****************************************************************************/
369 NTSTATUS cli_net_sam_logon(struct cli_state *cli, NET_ID_INFO_CTR *ctr,
370 NET_USER_INFO_3 *user_info3)
372 uint16 validation_level=3;
375 result = cli_net_sam_logon_internal(cli, ctr, user_info3,
378 if (NT_STATUS_IS_OK(result)) {
379 DEBUG(10,("cli_net_sam_logon: Success \n"));
380 } else if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_INVALID_INFO_CLASS)) {
381 DEBUG(10,("cli_net_sam_logon: STATUS INVALID INFO CLASS \n"));
386 * Since this is the second time we call this function, don't care
387 * for the error. If its error, return False.
390 result = cli_net_sam_logon_internal(cli, ctr, user_info3,
397 /***************************************************************************
400 This currently doesnt work correctly as the domain controller
401 returns NT_STATUS_INVALID_INFO_CLASS - we obviously need to
402 send a different info level. Right now though, I'm not sure
403 what that needs to be (I need to see one on the wire before
405 ****************************************************************************/
406 BOOL cli_net_sam_logoff(struct cli_state *cli, NET_ID_INFO_CTR *ctr)
408 DOM_CRED new_clnt_cred;
409 DOM_CRED dummy_rtn_creds;
412 NET_Q_SAM_LOGOFF q_s;
415 gen_next_creds( cli, &new_clnt_cred);
417 prs_init(&buf , 1024, cli->mem_ctx, MARSHALL);
418 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
420 /* create and send a MSRPC command with api NET_SAMLOGOFF */
422 DEBUG(4,("cli_net_sam_logoff: srv:%s mc:%s clnt %s %x ll: %d\n",
423 cli->srv_name_slash, global_myname,
424 credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time,
427 memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
429 init_sam_info(&q_s.sam_id, cli->srv_name_slash,
430 global_myname, &new_clnt_cred, &dummy_rtn_creds,
431 ctr->switch_value, ctr);
433 /* turn parameters into data stream */
434 if(!net_io_q_sam_logoff("", &q_s, &buf, 0)) {
435 DEBUG(0,("cli_net_sam_logoff: Error : failed to marshall NET_Q_SAM_LOGOFF struct.\n"));
441 /* send the data on \PIPE\ */
442 if (rpc_api_pipe_req(cli, NET_SAMLOGOFF, &buf, &rbuf))
444 NET_R_SAM_LOGOFF r_s;
446 ok = net_io_r_sam_logoff("", &r_s, &rbuf, 0);
448 if (ok && !NT_STATUS_IS_OK(r_s.status))
450 /* report error code */
451 DEBUG(0,("cli_net_sam_logoff: %s\n", get_nt_error_msg(r_s.status)));
455 /* Update the credentials. */
456 if (ok && !clnt_deal_with_creds(cli->sess_key, &(cli->clnt_cred), &(r_s.srv_creds)))
459 * Server replied with bad credential. Fail.
461 DEBUG(0,("cli_net_sam_logoff: server %s replied with bad credential (bad machine \
462 password ?).\n", cli->desthost ));