2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) Tim Potter 2001
6 Copyright (C) 2008 Guenther Deschner
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "utils/net.h"
23 #include "../libcli/auth/libcli_auth.h"
24 #include "../librpc/gen_ndr/ndr_lsa_c.h"
25 #include "rpc_client/cli_lsarpc.h"
26 #include "../librpc/gen_ndr/ndr_samr_c.h"
27 #include "rpc_client/init_samr.h"
28 #include "../librpc/gen_ndr/ndr_netlogon.h"
29 #include "rpc_client/cli_netlogon.h"
31 #include "rpc_client/init_lsa.h"
33 /* Macro for checking RPC error codes to make things more readable */
35 #define CHECK_RPC_ERR(rpc, msg) \
36 if (!NT_STATUS_IS_OK(status = rpc)) { \
37 DEBUG(0, (msg ": %s\n", nt_errstr(status))); \
41 #define CHECK_DCERPC_ERR(rpc, msg) \
42 if (!NT_STATUS_IS_OK(status = rpc)) { \
43 DEBUG(0, (msg ": %s\n", nt_errstr(status))); \
46 if (!NT_STATUS_IS_OK(result)) { \
48 DEBUG(0, (msg ": %s\n", nt_errstr(result))); \
53 #define CHECK_RPC_ERR_DEBUG(rpc, debug_args) \
54 if (!NT_STATUS_IS_OK(status = rpc)) { \
55 DEBUG(0, debug_args); \
59 #define CHECK_DCERPC_ERR_DEBUG(rpc, debug_args) \
60 if (!NT_STATUS_IS_OK(status = rpc)) { \
61 DEBUG(0, debug_args); \
64 if (!NT_STATUS_IS_OK(result)) { \
66 DEBUG(0, debug_args); \
72 * confirm that a domain join is still valid
74 * @return A shell status integer (0 for success)
77 NTSTATUS net_rpc_join_ok(struct net_context *c, const char *domain,
78 const char *server, struct sockaddr_storage *pss)
80 enum security_types sec;
81 unsigned int conn_flags = NET_FLAGS_PDC;
82 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
83 struct cli_state *cli = NULL;
84 struct rpc_pipe_client *pipe_hnd = NULL;
85 struct rpc_pipe_client *netlogon_pipe = NULL;
86 NTSTATUS ntret = NT_STATUS_UNSUCCESSFUL;
88 sec = (enum security_types)lp_security();
91 /* Connect to IPC$ using machine account's credentials. We don't use anonymous
92 connection here, as it may be denied by server's local policy. */
93 net_use_machine_account(c);
96 /* some servers (e.g. WinNT) don't accept machine-authenticated
98 conn_flags |= NET_FLAGS_ANONYMOUS;
101 /* Connect to remote machine */
102 ntret = net_make_ipc_connection_ex(c, domain, server, pss, conn_flags,
104 if (!NT_STATUS_IS_OK(ntret)) {
108 /* Setup the creds as though we're going to do schannel... */
109 ntret = get_schannel_session_key(cli, domain, &neg_flags,
112 /* We return NT_STATUS_INVALID_NETWORK_RESPONSE if the server is refusing
113 to negotiate schannel, but the creds were set up ok. That'll have to do. */
115 if (!NT_STATUS_IS_OK(ntret)) {
116 if (NT_STATUS_EQUAL(ntret, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
120 DEBUG(0,("net_rpc_join_ok: failed to get schannel session "
121 "key from server %s for domain %s. Error was %s\n",
122 cli->desthost, domain, nt_errstr(ntret) ));
128 /* Only do the rest of the schannel test if the client is allowed to do this. */
129 if (!lp_client_schannel()) {
135 ntret = cli_rpc_pipe_open_schannel_with_key(
136 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
137 DCERPC_AUTH_LEVEL_PRIVACY,
138 domain, &netlogon_pipe->dc, &pipe_hnd);
140 if (!NT_STATUS_IS_OK(ntret)) {
141 DEBUG(0,("net_rpc_join_ok: failed to open schannel session "
142 "on netlogon pipe to server %s for domain %s. Error was %s\n",
143 cli->desthost, domain, nt_errstr(ntret) ));
145 * Note: here, we have:
146 * (pipe_hnd != NULL) if and only if NT_STATUS_IS_OK(ntret)
155 * Join a domain using the administrator username and password
157 * @param argc Standard main() style argc
158 * @param argc Standard main() style argv. Initial components are already
159 * stripped. Currently not used.
160 * @return A shell status integer (0 for success)
164 int net_rpc_join_newstyle(struct net_context *c, int argc, const char **argv)
167 /* libsmb variables */
169 struct cli_state *cli;
171 uint32 acb_info = ACB_WSTRUST;
172 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
173 enum netr_SchannelType sec_channel_type;
174 struct rpc_pipe_client *pipe_hnd = NULL;
175 struct dcerpc_binding_handle *b = NULL;
179 struct policy_handle lsa_pol, sam_pol, domain_pol, user_pol;
180 struct dom_sid *domain_sid;
185 char *clear_trust_password = NULL;
186 struct samr_CryptPassword crypt_pwd;
187 uchar md4_trust_password[16];
188 union samr_UserInfo set_info;
192 NTSTATUS status, result;
194 const char *domain = NULL;
196 struct lsa_String lsa_acct_name;
198 uint32_t access_granted = 0;
199 union lsa_PolicyInformation *info = NULL;
200 struct samr_Ids user_rids;
201 struct samr_Ids name_types;
204 /* check what type of join */
206 sec_channel_type = get_sec_channel_type(argv[0]);
208 sec_channel_type = get_sec_channel_type(NULL);
211 switch (sec_channel_type) {
213 acb_info = ACB_WSTRUST;
216 acb_info = ACB_SVRTRUST;
219 case SEC_CHAN_DOMAIN:
220 acb_info = ACB_DOMTRUST;
224 DEBUG(0,("secure channel type %d not yet supported\n",
229 /* Make authenticated connection to remote machine */
231 status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
232 if (!NT_STATUS_IS_OK(status)) {
236 if (!(mem_ctx = talloc_init("net_rpc_join_newstyle"))) {
237 DEBUG(0, ("Could not initialise talloc context\n"));
241 /* Fetch domain sid */
243 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
245 if (!NT_STATUS_IS_OK(status)) {
246 DEBUG(0, ("Error connecting to LSA pipe. Error was %s\n",
247 nt_errstr(status) ));
251 b = pipe_hnd->binding_handle;
253 CHECK_RPC_ERR(rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
254 SEC_FLAG_MAXIMUM_ALLOWED,
256 "error opening lsa policy handle");
258 CHECK_DCERPC_ERR(dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
260 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
263 "error querying info policy");
265 domain = info->account_domain.name.string;
266 domain_sid = info->account_domain.sid;
268 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
269 TALLOC_FREE(pipe_hnd); /* Done with this pipe */
271 /* Bail out if domain didn't get set. */
273 DEBUG(0, ("Could not get domain name.\n"));
277 /* Create domain user */
278 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
280 if (!NT_STATUS_IS_OK(status)) {
281 DEBUG(0, ("Error connecting to SAM pipe. Error was %s\n",
282 nt_errstr(status) ));
286 b = pipe_hnd->binding_handle;
288 CHECK_DCERPC_ERR(dcerpc_samr_Connect2(b, mem_ctx,
290 SAMR_ACCESS_ENUM_DOMAINS
291 | SAMR_ACCESS_LOOKUP_DOMAIN,
294 "could not connect to SAM database");
297 CHECK_DCERPC_ERR(dcerpc_samr_OpenDomain(b, mem_ctx,
299 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
300 | SAMR_DOMAIN_ACCESS_CREATE_USER
301 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
305 "could not open domain");
307 /* Create domain user */
308 if ((acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname())) == NULL) {
309 status = NT_STATUS_NO_MEMORY;
312 strlower_m(acct_name);
314 init_lsa_String(&lsa_acct_name, acct_name);
316 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
317 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
318 SAMR_USER_ACCESS_SET_PASSWORD |
319 SAMR_USER_ACCESS_GET_ATTRIBUTES |
320 SAMR_USER_ACCESS_SET_ATTRIBUTES;
322 DEBUG(10, ("Creating account with flags: %d\n",acct_flags));
324 status = dcerpc_samr_CreateUser2(b, mem_ctx,
333 if (!NT_STATUS_IS_OK(status)) {
336 if (!NT_STATUS_IS_OK(result) &&
337 !NT_STATUS_EQUAL(result, NT_STATUS_USER_EXISTS)) {
339 d_fprintf(stderr,_("Creation of workstation account failed\n"));
341 /* If NT_STATUS_ACCESS_DENIED then we have a valid
342 username/password combo but the user does not have
343 administrator access. */
345 if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED))
346 d_fprintf(stderr, _("User specified does not have "
347 "administrator privileges\n"));
352 /* We *must* do this.... don't ask... */
354 if (NT_STATUS_IS_OK(result)) {
355 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
358 CHECK_DCERPC_ERR_DEBUG(dcerpc_samr_LookupNames(b, mem_ctx,
365 ("error looking up rid for user %s: %s/%s\n",
366 acct_name, nt_errstr(status), nt_errstr(result)));
368 if (name_types.ids[0] != SID_NAME_USER) {
369 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name, name_types.ids[0]));
373 user_rid = user_rids.ids[0];
375 /* Open handle on user */
377 CHECK_DCERPC_ERR_DEBUG(
378 dcerpc_samr_OpenUser(b, mem_ctx,
380 SEC_FLAG_MAXIMUM_ALLOWED,
384 ("could not re-open existing user %s: %s/%s\n",
385 acct_name, nt_errstr(status), nt_errstr(result)));
387 /* Create a random machine account password */
389 clear_trust_password = generate_random_str(talloc_tos(), DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
390 E_md4hash(clear_trust_password, md4_trust_password);
392 /* Set password on machine account */
394 init_samr_CryptPassword(clear_trust_password,
395 &cli->user_session_key,
398 set_info.info24.password = crypt_pwd;
399 set_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
401 CHECK_DCERPC_ERR(dcerpc_samr_SetUserInfo2(b, mem_ctx,
406 "error setting trust account password");
408 /* Why do we have to try to (re-)set the ACB to be the same as what
409 we passed in the samr_create_dom_user() call? When a NT
410 workstation is joined to a domain by an administrator the
411 acb_info is set to 0x80. For a normal user with "Add
412 workstations to the domain" rights the acb_info is 0x84. I'm
413 not sure whether it is supposed to make a difference or not. NT
414 seems to cope with either value so don't bomb out if the set
415 userinfo2 level 0x10 fails. -tpot */
417 set_info.info16.acct_flags = acb_info;
419 /* Ignoring the return value is necessary for joining a domain
420 as a normal user with "Add workstation to domain" privilege. */
422 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
428 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
429 TALLOC_FREE(pipe_hnd); /* Done with this pipe */
431 /* Now check the whole process from top-to-bottom */
433 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
435 if (!NT_STATUS_IS_OK(status)) {
436 DEBUG(0,("Error connecting to NETLOGON pipe. Error was %s\n",
437 nt_errstr(status) ));
441 status = rpccli_netlogon_setup_creds(pipe_hnd,
442 cli->desthost, /* server name */
444 global_myname(), /* client name */
445 global_myname(), /* machine account name */
450 if (!NT_STATUS_IS_OK(status)) {
451 DEBUG(0, ("Error in domain join verification (credential setup failed): %s\n\n",
454 if ( NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
455 (sec_channel_type == SEC_CHAN_BDC) ) {
456 d_fprintf(stderr, _("Please make sure that no computer "
457 "account\nnamed like this machine "
458 "(%s) exists in the domain\n"),
465 /* We can only check the schannel connection if the client is allowed
466 to do this and the server supports it. If not, just assume success
467 (after all the rpccli_netlogon_setup_creds() succeeded, and we'll
468 do the same again (setup creds) in net_rpc_join_ok(). JRA. */
470 if (lp_client_schannel() && (neg_flags & NETLOGON_NEG_SCHANNEL)) {
471 struct rpc_pipe_client *netlogon_schannel_pipe;
473 status = cli_rpc_pipe_open_schannel_with_key(
474 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
475 DCERPC_AUTH_LEVEL_PRIVACY, domain, &pipe_hnd->dc,
476 &netlogon_schannel_pipe);
478 if (!NT_STATUS_IS_OK(status)) {
479 DEBUG(0, ("Error in domain join verification (schannel setup failed): %s\n\n",
482 if ( NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
483 (sec_channel_type == SEC_CHAN_BDC) ) {
484 d_fprintf(stderr, _("Please make sure that no "
485 "computer account\nnamed "
486 "like this machine (%s) "
487 "exists in the domain\n"),
493 TALLOC_FREE(netlogon_schannel_pipe);
496 TALLOC_FREE(pipe_hnd);
498 /* Now store the secret in the secrets database */
500 strupper_m(CONST_DISCARD(char *, domain));
502 if (!secrets_store_domain_sid(domain, domain_sid)) {
503 DEBUG(0, ("error storing domain sid for %s\n", domain));
507 if (!secrets_store_machine_password(clear_trust_password, domain, sec_channel_type)) {
508 DEBUG(0, ("error storing plaintext domain secrets for %s\n", domain));
511 /* double-check, connection from scratch */
512 status = net_rpc_join_ok(c, domain, cli->desthost, &cli->dest_ss);
513 retval = NT_STATUS_IS_OK(status) ? 0 : -1;
517 /* Display success or failure */
521 fprintf(stderr,_("Unable to join domain %s.\n"),domain);
523 printf(_("Joined domain %s.\n"),domain);
529 TALLOC_FREE(clear_trust_password);
535 * check that a join is OK
537 * @return A shell status integer (0 for success)
540 int net_rpc_testjoin(struct net_context *c, int argc, const char **argv)
544 if (c->display_usage) {
547 " Test if a join is OK\n"));
551 /* Display success or failure */
552 nt_status = net_rpc_join_ok(c, c->opt_target_workgroup, NULL, NULL);
553 if (!NT_STATUS_IS_OK(nt_status)) {
554 fprintf(stderr, _("Join to domain '%s' is not valid: %s\n"),
555 c->opt_target_workgroup, nt_errstr(nt_status));
559 printf(_("Join to '%s' is OK\n"), c->opt_target_workgroup);