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.
32 extern int DEBUGLEVEL;
34 /****************************************************************************
35 Generate the next creds to use.
36 ****************************************************************************/
38 void gen_next_creds( struct cli_state *cli, DOM_CRED *new_clnt_cred)
41 * Create the new client credentials.
44 cli->clnt_cred.timestamp.time = time(NULL);
46 memcpy(new_clnt_cred, &cli->clnt_cred, sizeof(*new_clnt_cred));
48 /* Calculate the new credentials. */
49 cred_create(cli->sess_key, &(cli->clnt_cred.challenge),
50 new_clnt_cred->timestamp, &(new_clnt_cred->challenge));
54 /****************************************************************************
55 do a LSA Logon Control2
56 ****************************************************************************/
57 BOOL cli_net_logon_ctrl2(const char* srv_name, uint32 status_level)
61 NET_Q_LOGON_CTRL2 q_l;
64 struct cli_connection *con = NULL;
66 if (!cli_connection_init(srv_name, PIPE_NETLOGON, &con))
71 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
72 prs_init(&rbuf, 0, 4, SAFETY_MARGIN, True );
74 /* create and send a MSRPC command with api NET_LOGON_CTRL2 */
76 DEBUG(4,("net_logon_ctrl2 status level:%x\n", status_level));
78 /* store the parameters */
79 make_q_logon_ctrl2(&q_l, srv_name, 0, 0, status_level);
81 /* turn parameters into data stream */
82 net_io_q_logon_ctrl2("", &q_l, &buf, 0);
84 /* send the data on \PIPE\ */
85 if (rpc_con_pipe_req(con, NET_LOGON_CTRL2, &buf, &rbuf))
87 NET_R_LOGON_CTRL2 r_l;
89 net_io_r_logon_ctrl2("", &r_l, &rbuf, 0);
90 ok = (rbuf.offset != 0);
92 if (ok && r_l.status != 0)
94 /* report error code */
95 DEBUG(5,("net_logon_ctrl2: Error %s\n", get_nt_error_msg(r_l.status)));
103 cli_connection_unlink(con);
107 /****************************************************************************
110 Send the client credential, receive back a server credential.
111 Ensure that the server credential returned matches the session key
112 encrypt of the server challenge originally received. JRA.
113 ****************************************************************************/
115 uint32 cli_net_auth2(const char *srv_name,
116 const char *trust_acct,
117 const char *acct_name,
119 uint32 neg_flags, DOM_CHAL *srv_chal)
128 struct cli_connection *con = NULL;
130 if (!cli_connection_getsrv(srv_name, PIPE_NETLOGON, &con))
135 if (!cli_get_con_sesskey(con, sess_key))
140 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
141 prs_init(&rbuf, 0, 4, SAFETY_MARGIN, True );
143 /* create and send a MSRPC command with api NET_AUTH2 */
145 DEBUG(4,("cli_net_auth2: srv:%s acct:%s sc:%x mc: %s neg: %x\n",
146 srv_name, trust_acct, sec_chan, acct_name,
149 cli_con_get_cli_cred(con, &clnt_cred);
151 /* store the parameters */
152 make_q_auth_2(&q_a, srv_name, trust_acct, sec_chan, acct_name,
153 &clnt_cred.challenge, neg_flags);
155 /* turn parameters into data stream */
156 net_io_q_auth_2("", &q_a, &buf, 0);
158 /* send the data on \PIPE\ */
159 if (rpc_con_pipe_req(con, NET_AUTH2, &buf, &rbuf))
163 net_io_r_auth_2("", &r_a, &rbuf, 0);
164 status = (rbuf.offset == 0) ? 0xC0000000 | NT_STATUS_INVALID_PARAMETER : 0;
166 if (status == 0x0 && r_a.status != 0)
168 /* report error code */
169 DEBUG(5,("cli_net_auth2: Error %s\n",
170 get_nt_error_msg(r_a.status)));
177 * Check the returned value using the initial
178 * server received challenge.
183 if(cred_assert( &r_a.srv_chal, sess_key,
184 srv_chal, zerotime) == 0)
187 * Server replied with bad credential. Fail.
189 DEBUG(5,("cli_net_auth2: server %s replied \
190 with bad credential (bad trust account password ?).\n", srv_name));
191 status = NT_STATUS_NETWORK_CREDENTIAL_CONFLICT | 0xC0000000;
197 * Try commenting this out to see if this makes the connect
198 * work for a NT 3.51 PDC. JRA.
201 if (ok && r_a.srv_flgs.neg_flags != q_a.clnt_flgs.neg_flags)
203 /* report different neg_flags */
204 DEBUG(5,("cli_net_auth2: error neg_flags (q,r) differ - (%x,%x)\n",
205 q_a.clnt_flgs.neg_flags, r_a.srv_flgs.neg_flags));
213 DEBUG(5,("rpc_con_pipe_req FAILED\n"));
214 status = 0xC0000000 | NT_STATUS_ACCESS_DENIED;
217 DEBUG(5,("cli_net_auth2 status: %x\n", status));
225 /****************************************************************************
226 LSA Request Challenge. Sends our challenge to server, then gets
227 server response. These are used to generate the credentials.
228 ****************************************************************************/
230 uint32 cli_net_req_chal( const char *srv_name, const char* myhostname,
231 DOM_CHAL *clnt_chal, DOM_CHAL *srv_chal)
238 struct cli_connection *con = NULL;
240 if (!cli_connection_init(srv_name, PIPE_NETLOGON, &con))
245 if (srv_chal == NULL || clnt_chal == NULL)
246 return 0xC0000000 | NT_STATUS_INVALID_PARAMETER;
248 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
249 prs_init(&rbuf, 0, 4, SAFETY_MARGIN, True );
251 /* create and send a MSRPC command with api NET_REQCHAL */
253 DEBUG(4,("cli_net_req_chal: LSA Request Challenge from %s to %s: %s\n",
254 srv_name, myhostname, credstr(clnt_chal->data)));
256 /* store the parameters */
257 make_q_req_chal(&q_c, srv_name, myhostname, clnt_chal);
259 /* turn parameters into data stream */
260 net_io_q_req_chal("", &q_c, &buf, 0);
262 /* send the data on \PIPE\ */
263 if (rpc_con_pipe_req(con, NET_REQCHAL, &buf, &rbuf))
267 net_io_r_req_chal("", &r_c, &rbuf, 0);
268 status = (rbuf.offset == 0) ? 0xC0000000 | NT_STATUS_INVALID_PARAMETER : 0;
270 if (status == 0x0 && r_c.status != 0)
272 /* report error code */
273 DEBUG(5,("cli_net_req_chal: Error %s\n", get_nt_error_msg(r_c.status)));
279 /* ok, at last: we're happy. return the challenge */
280 memcpy(srv_chal, r_c.srv_chal.data, sizeof(srv_chal->data));
285 DEBUG(5,("rpc_con_pipe_req FAILED\n"));
286 status = 0xC0000000 | NT_STATUS_ACCESS_DENIED;
295 /***************************************************************************
296 LSA Server Password Set.
297 ****************************************************************************/
299 BOOL cli_net_srv_pwset(const char* srv_name,
300 const char* myhostname,
301 const char* trust_acct,
302 uint8 hashed_trust_pwd[16],
303 uint16 sec_chan_type)
307 DOM_CRED new_clnt_cred;
310 unsigned char processed_new_pwd[16];
311 /* Process the new password. */
315 struct cli_connection *con = NULL;
317 if (!cli_connection_getsrv(srv_name, PIPE_NETLOGON, &con))
322 if (!cli_get_con_sesskey(con, sess_key))
327 cred_hash3( processed_new_pwd, hashed_trust_pwd, sess_key, 1);
329 cli_con_gen_next_creds( con, &new_clnt_cred);
331 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
332 prs_init(&rbuf, 0, 4, SAFETY_MARGIN, True );
334 /* create and send a MSRPC command with api NET_SRV_PWSET */
336 DEBUG(4,("cli_net_srv_pwset: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
337 srv_name, trust_acct, sec_chan_type, myhostname,
338 credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time));
340 /* store the parameters */
341 make_q_srv_pwset(&q_s, srv_name, trust_acct, sec_chan_type,
342 myhostname, &new_clnt_cred, (char *)processed_new_pwd);
344 /* turn parameters into data stream */
345 net_io_q_srv_pwset("", &q_s, &buf, 0);
347 /* send the data on \PIPE\ */
348 if (rpc_con_pipe_req(con, NET_SRVPWSET, &buf, &rbuf))
352 net_io_r_srv_pwset("", &r_s, &rbuf, 0);
353 ok = (rbuf.offset != 0);
355 if (ok && r_s.status != 0)
357 /* report error code */
358 DEBUG(5,("cli_net_srv_pwset: %s\n", get_nt_error_msg(r_s.status)));
362 /* Update the credentials. */
363 if (ok && !cli_con_deal_with_creds(con, &(r_s.srv_cred)))
366 * Server replied with bad credential. Fail.
368 DEBUG(5,("cli_net_srv_pwset: server %s replied with bad credential \
369 (bad trust account password ?).\n", srv_name));
380 /***************************************************************************
381 LSA SAM Logon - interactive or network.
382 ****************************************************************************/
384 BOOL cli_net_sam_logon(const char* srv_name, const char* myhostname,
385 NET_ID_INFO_CTR *ctr,
386 NET_USER_INFO_3 *user_info3)
388 DOM_CRED new_clnt_cred;
389 DOM_CRED dummy_rtn_creds;
392 uint16 validation_level = 3;
396 struct cli_connection *con = NULL;
398 if (!cli_connection_getsrv(srv_name, PIPE_NETLOGON, &con))
403 cli_con_gen_next_creds( con, &new_clnt_cred);
405 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
406 prs_init(&rbuf, 0, 4, SAFETY_MARGIN, True );
408 /* create and send a MSRPC command with api NET_SAMLOGON */
410 DEBUG(4,("cli_net_sam_logon: srv:%s mc:%s ll: %d\n",
411 srv_name, myhostname,
414 memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
415 dummy_rtn_creds.timestamp.time = time(NULL);
417 /* store the parameters */
418 make_sam_info(&(q_s.sam_id), srv_name, myhostname,
419 &new_clnt_cred, &dummy_rtn_creds, ctr->switch_value, ctr, validation_level);
421 /* turn parameters into data stream */
422 net_io_q_sam_logon("", &q_s, &buf, 0);
424 /* send the data on \PIPE\ */
425 if (rpc_con_pipe_req(con, NET_SAMLOGON, &buf, &rbuf))
429 r_s.user = user_info3;
431 net_io_r_sam_logon("", &r_s, &rbuf, 0);
432 ok = (rbuf.offset != 0);
434 if (ok && r_s.status != 0)
436 /* report error code */
437 DEBUG(5,("cli_net_sam_logon: %s\n", get_nt_error_msg(r_s.status)));
441 /* Update the credentials. */
442 if (ok && !cli_con_deal_with_creds(con, &(r_s.srv_creds)))
445 * Server replied with bad credential. Fail.
447 DEBUG(5,("cli_net_sam_logon: server %s replied with bad credential \
448 (bad trust account password ?).\n", srv_name));
452 if (ok && r_s.switch_value != 3)
454 /* report different switch_value */
455 DEBUG(5,("cli_net_sam_logon: switch_value of 3 expected %x\n",
467 /***************************************************************************
470 This currently doesnt work correctly as the domain controller
471 returns NT_STATUS_INVALID_INFO_CLASS - we obviously need to
472 send a different info level. Right now though, I'm not sure
473 what that needs to be (I need to see one on the wire before
475 ****************************************************************************/
476 BOOL cli_net_sam_logoff(const char* srv_name, const char* myhostname,
477 NET_ID_INFO_CTR *ctr)
479 DOM_CRED new_clnt_cred;
480 DOM_CRED dummy_rtn_creds;
483 NET_Q_SAM_LOGOFF q_s;
484 uint16 validation_level = 3;
487 struct cli_connection *con = NULL;
489 if (!cli_connection_getsrv(srv_name, PIPE_NETLOGON, &con))
494 cli_con_gen_next_creds( con, &new_clnt_cred);
496 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
497 prs_init(&rbuf, 0, 4, SAFETY_MARGIN, True );
499 /* create and send a MSRPC command with api NET_SAMLOGOFF */
501 DEBUG(4,("cli_net_sam_logoff: srv:%s mc:%s clnt %s %x ll: %d\n",
502 srv_name, myhostname,
503 credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time,
506 memset(&dummy_rtn_creds, '\0', sizeof(dummy_rtn_creds));
508 /* store the parameters */
509 make_sam_info(&(q_s.sam_id), srv_name, myhostname,
510 &new_clnt_cred, &dummy_rtn_creds, ctr->switch_value, ctr, validation_level);
512 /* turn parameters into data stream */
513 net_io_q_sam_logoff("", &q_s, &buf, 0);
515 /* send the data on \PIPE\ */
516 if (rpc_con_pipe_req(con, NET_SAMLOGOFF, &buf, &rbuf))
518 NET_R_SAM_LOGOFF r_s;
520 net_io_r_sam_logoff("", &r_s, &rbuf, 0);
521 ok = (rbuf.offset != 0);
523 if (ok && r_s.status != 0)
525 /* report error code */
526 DEBUG(5,("cli_net_sam_logoff: %s\n", get_nt_error_msg(r_s.status)));
530 /* Update the credentials. */
531 if (ok && !cli_con_deal_with_creds(con, &(r_s.srv_creds)))
534 * Server replied with bad credential. Fail.
536 DEBUG(5,("cli_net_sam_logoff: server %s replied with bad credential \
537 (bad trust account password ?).\n", srv_name ));
548 /***************************************************************************
549 Synchronise SAM Database (requires SEC_CHAN_BDC).
550 ****************************************************************************/
551 BOOL cli_net_sam_sync( const char* srv_name, const char* myhostname,
554 SAM_DELTA_HDR *hdr_deltas,
555 SAM_DELTA_CTR *deltas)
560 DOM_CRED new_clnt_cred;
564 struct cli_connection *con = NULL;
566 if (!cli_connection_getsrv(srv_name, PIPE_NETLOGON, &con))
571 if (!cli_get_con_sesskey(con, sess_key))
576 cli_con_gen_next_creds(con, &new_clnt_cred);
578 prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
579 prs_init(&rbuf, 0, 4, SAFETY_MARGIN, True );
581 /* create and send a MSRPC command with api NET_SAM_SYNC */
583 make_q_sam_sync(&q_s, srv_name, myhostname,
584 &new_clnt_cred, database_id);
586 /* turn parameters into data stream */
587 net_io_q_sam_sync("", &q_s, &buf, 0);
589 /* send the data on \PIPE\ */
590 if (rpc_con_pipe_req(con, NET_SAM_SYNC, &buf, &rbuf))
594 r_s.hdr_deltas = hdr_deltas;
597 net_io_r_sam_sync("", sess_key, &r_s, &rbuf, 0);
598 ok = (rbuf.offset != 0);
600 if (ok && r_s.status != 0 && r_s.status != STATUS_MORE_ENTRIES)
602 /* report error code */
603 DEBUG(5,("cli_net_sam_sync: %s\n", get_nt_error_msg(r_s.status)));
607 /* Update the credentials. */
608 if (ok && !cli_con_deal_with_creds(con, &(r_s.srv_creds)))
610 DEBUG(5,("cli_net_sam_sync: server %s replied with bad \
611 credential (bad trust account password ?).\n", srv_name));
617 *num_deltas = r_s.num_deltas2;
619 if (r_s.status == STATUS_MORE_ENTRIES)
621 DEBUG(5, ("(More entries)\n"));