1 | /*
|
---|
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
|
---|
7 |
|
---|
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.
|
---|
12 |
|
---|
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.
|
---|
17 |
|
---|
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/>. */
|
---|
20 |
|
---|
21 | #include "includes.h"
|
---|
22 | #include "utils/net.h"
|
---|
23 | #include "rpc_client/cli_pipe.h"
|
---|
24 | #include "../libcli/auth/libcli_auth.h"
|
---|
25 | #include "../librpc/gen_ndr/ndr_lsa_c.h"
|
---|
26 | #include "rpc_client/cli_lsarpc.h"
|
---|
27 | #include "../librpc/gen_ndr/ndr_samr_c.h"
|
---|
28 | #include "rpc_client/init_samr.h"
|
---|
29 | #include "../librpc/gen_ndr/ndr_netlogon.h"
|
---|
30 | #include "rpc_client/cli_netlogon.h"
|
---|
31 | #include "secrets.h"
|
---|
32 | #include "rpc_client/init_lsa.h"
|
---|
33 | #include "libsmb/libsmb.h"
|
---|
34 |
|
---|
35 | /* Macro for checking RPC error codes to make things more readable */
|
---|
36 |
|
---|
37 | #define CHECK_RPC_ERR(rpc, msg) \
|
---|
38 | if (!NT_STATUS_IS_OK(status = rpc)) { \
|
---|
39 | DEBUG(0, (msg ": %s\n", nt_errstr(status))); \
|
---|
40 | goto done; \
|
---|
41 | }
|
---|
42 |
|
---|
43 | #define CHECK_DCERPC_ERR(rpc, msg) \
|
---|
44 | if (!NT_STATUS_IS_OK(status = rpc)) { \
|
---|
45 | DEBUG(0, (msg ": %s\n", nt_errstr(status))); \
|
---|
46 | goto done; \
|
---|
47 | } \
|
---|
48 | if (!NT_STATUS_IS_OK(result)) { \
|
---|
49 | status = result; \
|
---|
50 | DEBUG(0, (msg ": %s\n", nt_errstr(result))); \
|
---|
51 | goto done; \
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | #define CHECK_RPC_ERR_DEBUG(rpc, debug_args) \
|
---|
56 | if (!NT_STATUS_IS_OK(status = rpc)) { \
|
---|
57 | DEBUG(0, debug_args); \
|
---|
58 | goto done; \
|
---|
59 | }
|
---|
60 |
|
---|
61 | #define CHECK_DCERPC_ERR_DEBUG(rpc, debug_args) \
|
---|
62 | if (!NT_STATUS_IS_OK(status = rpc)) { \
|
---|
63 | DEBUG(0, debug_args); \
|
---|
64 | goto done; \
|
---|
65 | } \
|
---|
66 | if (!NT_STATUS_IS_OK(result)) { \
|
---|
67 | status = result; \
|
---|
68 | DEBUG(0, debug_args); \
|
---|
69 | goto done; \
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * confirm that a domain join is still valid
|
---|
75 | *
|
---|
76 | * @return A shell status integer (0 for success)
|
---|
77 | *
|
---|
78 | **/
|
---|
79 | NTSTATUS net_rpc_join_ok(struct net_context *c, const char *domain,
|
---|
80 | const char *server, struct sockaddr_storage *pss)
|
---|
81 | {
|
---|
82 | enum security_types sec;
|
---|
83 | unsigned int conn_flags = NET_FLAGS_PDC;
|
---|
84 | uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
|
---|
85 | struct cli_state *cli = NULL;
|
---|
86 | struct rpc_pipe_client *pipe_hnd = NULL;
|
---|
87 | struct rpc_pipe_client *netlogon_pipe = NULL;
|
---|
88 | NTSTATUS ntret = NT_STATUS_UNSUCCESSFUL;
|
---|
89 |
|
---|
90 | sec = (enum security_types)lp_security();
|
---|
91 |
|
---|
92 | if (sec == SEC_ADS) {
|
---|
93 | /* Connect to IPC$ using machine account's credentials. We don't use anonymous
|
---|
94 | connection here, as it may be denied by server's local policy. */
|
---|
95 | net_use_machine_account(c);
|
---|
96 |
|
---|
97 | } else {
|
---|
98 | /* some servers (e.g. WinNT) don't accept machine-authenticated
|
---|
99 | smb connections */
|
---|
100 | conn_flags |= NET_FLAGS_ANONYMOUS;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /* Connect to remote machine */
|
---|
104 | ntret = net_make_ipc_connection_ex(c, domain, server, pss, conn_flags,
|
---|
105 | &cli);
|
---|
106 | if (!NT_STATUS_IS_OK(ntret)) {
|
---|
107 | return ntret;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /* Setup the creds as though we're going to do schannel... */
|
---|
111 | ntret = get_schannel_session_key(cli, domain, &neg_flags,
|
---|
112 | &netlogon_pipe);
|
---|
113 |
|
---|
114 | /* We return NT_STATUS_INVALID_NETWORK_RESPONSE if the server is refusing
|
---|
115 | to negotiate schannel, but the creds were set up ok. That'll have to do. */
|
---|
116 |
|
---|
117 | if (!NT_STATUS_IS_OK(ntret)) {
|
---|
118 | if (NT_STATUS_EQUAL(ntret, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
|
---|
119 | cli_shutdown(cli);
|
---|
120 | return NT_STATUS_OK;
|
---|
121 | } else {
|
---|
122 | DEBUG(0,("net_rpc_join_ok: failed to get schannel session "
|
---|
123 | "key from server %s for domain %s. Error was %s\n",
|
---|
124 | cli->desthost, domain, nt_errstr(ntret) ));
|
---|
125 | cli_shutdown(cli);
|
---|
126 | return ntret;
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | /* Only do the rest of the schannel test if the client is allowed to do this. */
|
---|
131 | if (!lp_client_schannel()) {
|
---|
132 | cli_shutdown(cli);
|
---|
133 | /* We're good... */
|
---|
134 | return ntret;
|
---|
135 | }
|
---|
136 |
|
---|
137 | ntret = cli_rpc_pipe_open_schannel_with_key(
|
---|
138 | cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
|
---|
139 | DCERPC_AUTH_LEVEL_PRIVACY,
|
---|
140 | domain, &netlogon_pipe->dc, &pipe_hnd);
|
---|
141 |
|
---|
142 | if (!NT_STATUS_IS_OK(ntret)) {
|
---|
143 | DEBUG(0,("net_rpc_join_ok: failed to open schannel session "
|
---|
144 | "on netlogon pipe to server %s for domain %s. Error was %s\n",
|
---|
145 | cli->desthost, domain, nt_errstr(ntret) ));
|
---|
146 | /*
|
---|
147 | * Note: here, we have:
|
---|
148 | * (pipe_hnd != NULL) if and only if NT_STATUS_IS_OK(ntret)
|
---|
149 | */
|
---|
150 | }
|
---|
151 |
|
---|
152 | cli_shutdown(cli);
|
---|
153 | return ntret;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Join a domain using the administrator username and password
|
---|
158 | *
|
---|
159 | * @param argc Standard main() style argc
|
---|
160 | * @param argc Standard main() style argv. Initial components are already
|
---|
161 | * stripped. Currently not used.
|
---|
162 | * @return A shell status integer (0 for success)
|
---|
163 | *
|
---|
164 | **/
|
---|
165 |
|
---|
166 | int net_rpc_join_newstyle(struct net_context *c, int argc, const char **argv)
|
---|
167 | {
|
---|
168 |
|
---|
169 | /* libsmb variables */
|
---|
170 |
|
---|
171 | struct cli_state *cli;
|
---|
172 | TALLOC_CTX *mem_ctx;
|
---|
173 | uint32 acb_info = ACB_WSTRUST;
|
---|
174 | uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
|
---|
175 | enum netr_SchannelType sec_channel_type;
|
---|
176 | struct rpc_pipe_client *pipe_hnd = NULL;
|
---|
177 | struct dcerpc_binding_handle *b = NULL;
|
---|
178 |
|
---|
179 | /* rpc variables */
|
---|
180 |
|
---|
181 | struct policy_handle lsa_pol, sam_pol, domain_pol, user_pol;
|
---|
182 | struct dom_sid *domain_sid;
|
---|
183 | uint32 user_rid;
|
---|
184 |
|
---|
185 | /* Password stuff */
|
---|
186 |
|
---|
187 | char *clear_trust_password = NULL;
|
---|
188 | struct samr_CryptPassword crypt_pwd;
|
---|
189 | uchar md4_trust_password[16];
|
---|
190 | union samr_UserInfo set_info;
|
---|
191 |
|
---|
192 | /* Misc */
|
---|
193 |
|
---|
194 | NTSTATUS status, result;
|
---|
195 | int retval = 1;
|
---|
196 | const char *domain = NULL;
|
---|
197 | char *acct_name;
|
---|
198 | struct lsa_String lsa_acct_name;
|
---|
199 | uint32 acct_flags=0;
|
---|
200 | uint32_t access_granted = 0;
|
---|
201 | union lsa_PolicyInformation *info = NULL;
|
---|
202 | struct samr_Ids user_rids;
|
---|
203 | struct samr_Ids name_types;
|
---|
204 |
|
---|
205 |
|
---|
206 | /* check what type of join */
|
---|
207 | if (argc >= 0) {
|
---|
208 | sec_channel_type = get_sec_channel_type(argv[0]);
|
---|
209 | } else {
|
---|
210 | sec_channel_type = get_sec_channel_type(NULL);
|
---|
211 | }
|
---|
212 |
|
---|
213 | switch (sec_channel_type) {
|
---|
214 | case SEC_CHAN_WKSTA:
|
---|
215 | acb_info = ACB_WSTRUST;
|
---|
216 | break;
|
---|
217 | case SEC_CHAN_BDC:
|
---|
218 | acb_info = ACB_SVRTRUST;
|
---|
219 | break;
|
---|
220 | #if 0
|
---|
221 | case SEC_CHAN_DOMAIN:
|
---|
222 | acb_info = ACB_DOMTRUST;
|
---|
223 | break;
|
---|
224 | #endif
|
---|
225 | default:
|
---|
226 | DEBUG(0,("secure channel type %d not yet supported\n",
|
---|
227 | sec_channel_type));
|
---|
228 | break;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /* Make authenticated connection to remote machine */
|
---|
232 |
|
---|
233 | status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
|
---|
234 | if (!NT_STATUS_IS_OK(status)) {
|
---|
235 | return 1;
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (!(mem_ctx = talloc_init("net_rpc_join_newstyle"))) {
|
---|
239 | DEBUG(0, ("Could not initialise talloc context\n"));
|
---|
240 | goto done;
|
---|
241 | }
|
---|
242 |
|
---|
243 | /* Fetch domain sid */
|
---|
244 |
|
---|
245 | status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
|
---|
246 | &pipe_hnd);
|
---|
247 | if (!NT_STATUS_IS_OK(status)) {
|
---|
248 | DEBUG(0, ("Error connecting to LSA pipe. Error was %s\n",
|
---|
249 | nt_errstr(status) ));
|
---|
250 | goto done;
|
---|
251 | }
|
---|
252 |
|
---|
253 | b = pipe_hnd->binding_handle;
|
---|
254 |
|
---|
255 | CHECK_RPC_ERR(rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
|
---|
256 | SEC_FLAG_MAXIMUM_ALLOWED,
|
---|
257 | &lsa_pol),
|
---|
258 | "error opening lsa policy handle");
|
---|
259 |
|
---|
260 | CHECK_DCERPC_ERR(dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
|
---|
261 | &lsa_pol,
|
---|
262 | LSA_POLICY_INFO_ACCOUNT_DOMAIN,
|
---|
263 | &info,
|
---|
264 | &result),
|
---|
265 | "error querying info policy");
|
---|
266 |
|
---|
267 | domain = info->account_domain.name.string;
|
---|
268 | domain_sid = info->account_domain.sid;
|
---|
269 |
|
---|
270 | dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
|
---|
271 | TALLOC_FREE(pipe_hnd); /* Done with this pipe */
|
---|
272 |
|
---|
273 | /* Bail out if domain didn't get set. */
|
---|
274 | if (!domain) {
|
---|
275 | DEBUG(0, ("Could not get domain name.\n"));
|
---|
276 | goto done;
|
---|
277 | }
|
---|
278 |
|
---|
279 | /* Create domain user */
|
---|
280 | status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
|
---|
281 | &pipe_hnd);
|
---|
282 | if (!NT_STATUS_IS_OK(status)) {
|
---|
283 | DEBUG(0, ("Error connecting to SAM pipe. Error was %s\n",
|
---|
284 | nt_errstr(status) ));
|
---|
285 | goto done;
|
---|
286 | }
|
---|
287 |
|
---|
288 | b = pipe_hnd->binding_handle;
|
---|
289 |
|
---|
290 | CHECK_DCERPC_ERR(dcerpc_samr_Connect2(b, mem_ctx,
|
---|
291 | pipe_hnd->desthost,
|
---|
292 | SAMR_ACCESS_ENUM_DOMAINS
|
---|
293 | | SAMR_ACCESS_LOOKUP_DOMAIN,
|
---|
294 | &sam_pol,
|
---|
295 | &result),
|
---|
296 | "could not connect to SAM database");
|
---|
297 |
|
---|
298 |
|
---|
299 | CHECK_DCERPC_ERR(dcerpc_samr_OpenDomain(b, mem_ctx,
|
---|
300 | &sam_pol,
|
---|
301 | SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
|
---|
302 | | SAMR_DOMAIN_ACCESS_CREATE_USER
|
---|
303 | | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
|
---|
304 | domain_sid,
|
---|
305 | &domain_pol,
|
---|
306 | &result),
|
---|
307 | "could not open domain");
|
---|
308 |
|
---|
309 | /* Create domain user */
|
---|
310 | if ((acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname())) == NULL) {
|
---|
311 | status = NT_STATUS_NO_MEMORY;
|
---|
312 | goto done;
|
---|
313 | }
|
---|
314 | strlower_m(acct_name);
|
---|
315 |
|
---|
316 | init_lsa_String(&lsa_acct_name, acct_name);
|
---|
317 |
|
---|
318 | acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
|
---|
319 | SEC_STD_WRITE_DAC | SEC_STD_DELETE |
|
---|
320 | SAMR_USER_ACCESS_SET_PASSWORD |
|
---|
321 | SAMR_USER_ACCESS_GET_ATTRIBUTES |
|
---|
322 | SAMR_USER_ACCESS_SET_ATTRIBUTES;
|
---|
323 |
|
---|
324 | DEBUG(10, ("Creating account with flags: %d\n",acct_flags));
|
---|
325 |
|
---|
326 | status = dcerpc_samr_CreateUser2(b, mem_ctx,
|
---|
327 | &domain_pol,
|
---|
328 | &lsa_acct_name,
|
---|
329 | acb_info,
|
---|
330 | acct_flags,
|
---|
331 | &user_pol,
|
---|
332 | &access_granted,
|
---|
333 | &user_rid,
|
---|
334 | &result);
|
---|
335 | if (!NT_STATUS_IS_OK(status)) {
|
---|
336 | goto done;
|
---|
337 | }
|
---|
338 | if (!NT_STATUS_IS_OK(result) &&
|
---|
339 | !NT_STATUS_EQUAL(result, NT_STATUS_USER_EXISTS)) {
|
---|
340 | status = result;
|
---|
341 | d_fprintf(stderr,_("Creation of workstation account failed\n"));
|
---|
342 |
|
---|
343 | /* If NT_STATUS_ACCESS_DENIED then we have a valid
|
---|
344 | username/password combo but the user does not have
|
---|
345 | administrator access. */
|
---|
346 |
|
---|
347 | if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED))
|
---|
348 | d_fprintf(stderr, _("User specified does not have "
|
---|
349 | "administrator privileges\n"));
|
---|
350 |
|
---|
351 | goto done;
|
---|
352 | }
|
---|
353 |
|
---|
354 | /* We *must* do this.... don't ask... */
|
---|
355 |
|
---|
356 | if (NT_STATUS_IS_OK(result)) {
|
---|
357 | dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
|
---|
358 | }
|
---|
359 |
|
---|
360 | CHECK_DCERPC_ERR_DEBUG(dcerpc_samr_LookupNames(b, mem_ctx,
|
---|
361 | &domain_pol,
|
---|
362 | 1,
|
---|
363 | &lsa_acct_name,
|
---|
364 | &user_rids,
|
---|
365 | &name_types,
|
---|
366 | &result),
|
---|
367 | ("error looking up rid for user %s: %s/%s\n",
|
---|
368 | acct_name, nt_errstr(status), nt_errstr(result)));
|
---|
369 |
|
---|
370 | if (user_rids.count != 1) {
|
---|
371 | status = NT_STATUS_INVALID_NETWORK_RESPONSE;
|
---|
372 | goto done;
|
---|
373 | }
|
---|
374 | if (name_types.count != 1) {
|
---|
375 | status = NT_STATUS_INVALID_NETWORK_RESPONSE;
|
---|
376 | goto done;
|
---|
377 | }
|
---|
378 |
|
---|
379 | if (name_types.ids[0] != SID_NAME_USER) {
|
---|
380 | DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name, name_types.ids[0]));
|
---|
381 | goto done;
|
---|
382 | }
|
---|
383 |
|
---|
384 | user_rid = user_rids.ids[0];
|
---|
385 |
|
---|
386 | /* Open handle on user */
|
---|
387 |
|
---|
388 | CHECK_DCERPC_ERR_DEBUG(
|
---|
389 | dcerpc_samr_OpenUser(b, mem_ctx,
|
---|
390 | &domain_pol,
|
---|
391 | SEC_FLAG_MAXIMUM_ALLOWED,
|
---|
392 | user_rid,
|
---|
393 | &user_pol,
|
---|
394 | &result),
|
---|
395 | ("could not re-open existing user %s: %s/%s\n",
|
---|
396 | acct_name, nt_errstr(status), nt_errstr(result)));
|
---|
397 |
|
---|
398 | /* Create a random machine account password */
|
---|
399 |
|
---|
400 | clear_trust_password = generate_random_str(talloc_tos(), DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
|
---|
401 | E_md4hash(clear_trust_password, md4_trust_password);
|
---|
402 |
|
---|
403 | /* Set password on machine account */
|
---|
404 |
|
---|
405 | init_samr_CryptPassword(clear_trust_password,
|
---|
406 | &cli->user_session_key,
|
---|
407 | &crypt_pwd);
|
---|
408 |
|
---|
409 | set_info.info24.password = crypt_pwd;
|
---|
410 | set_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
|
---|
411 |
|
---|
412 | CHECK_DCERPC_ERR(dcerpc_samr_SetUserInfo2(b, mem_ctx,
|
---|
413 | &user_pol,
|
---|
414 | 24,
|
---|
415 | &set_info,
|
---|
416 | &result),
|
---|
417 | "error setting trust account password");
|
---|
418 |
|
---|
419 | /* Why do we have to try to (re-)set the ACB to be the same as what
|
---|
420 | we passed in the samr_create_dom_user() call? When a NT
|
---|
421 | workstation is joined to a domain by an administrator the
|
---|
422 | acb_info is set to 0x80. For a normal user with "Add
|
---|
423 | workstations to the domain" rights the acb_info is 0x84. I'm
|
---|
424 | not sure whether it is supposed to make a difference or not. NT
|
---|
425 | seems to cope with either value so don't bomb out if the set
|
---|
426 | userinfo2 level 0x10 fails. -tpot */
|
---|
427 |
|
---|
428 | set_info.info16.acct_flags = acb_info;
|
---|
429 |
|
---|
430 | /* Ignoring the return value is necessary for joining a domain
|
---|
431 | as a normal user with "Add workstation to domain" privilege. */
|
---|
432 |
|
---|
433 | status = dcerpc_samr_SetUserInfo(b, mem_ctx,
|
---|
434 | &user_pol,
|
---|
435 | 16,
|
---|
436 | &set_info,
|
---|
437 | &result);
|
---|
438 |
|
---|
439 | dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
|
---|
440 | TALLOC_FREE(pipe_hnd); /* Done with this pipe */
|
---|
441 |
|
---|
442 | /* Now check the whole process from top-to-bottom */
|
---|
443 |
|
---|
444 | status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
|
---|
445 | &pipe_hnd);
|
---|
446 | if (!NT_STATUS_IS_OK(status)) {
|
---|
447 | DEBUG(0,("Error connecting to NETLOGON pipe. Error was %s\n",
|
---|
448 | nt_errstr(status) ));
|
---|
449 | goto done;
|
---|
450 | }
|
---|
451 |
|
---|
452 | status = rpccli_netlogon_setup_creds(pipe_hnd,
|
---|
453 | cli->desthost, /* server name */
|
---|
454 | domain, /* domain */
|
---|
455 | global_myname(), /* client name */
|
---|
456 | global_myname(), /* machine account name */
|
---|
457 | md4_trust_password,
|
---|
458 | sec_channel_type,
|
---|
459 | &neg_flags);
|
---|
460 |
|
---|
461 | if (!NT_STATUS_IS_OK(status)) {
|
---|
462 | DEBUG(0, ("Error in domain join verification (credential setup failed): %s\n\n",
|
---|
463 | nt_errstr(status)));
|
---|
464 |
|
---|
465 | if ( NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
|
---|
466 | (sec_channel_type == SEC_CHAN_BDC) ) {
|
---|
467 | d_fprintf(stderr, _("Please make sure that no computer "
|
---|
468 | "account\nnamed like this machine "
|
---|
469 | "(%s) exists in the domain\n"),
|
---|
470 | global_myname());
|
---|
471 | }
|
---|
472 |
|
---|
473 | goto done;
|
---|
474 | }
|
---|
475 |
|
---|
476 | /* We can only check the schannel connection if the client is allowed
|
---|
477 | to do this and the server supports it. If not, just assume success
|
---|
478 | (after all the rpccli_netlogon_setup_creds() succeeded, and we'll
|
---|
479 | do the same again (setup creds) in net_rpc_join_ok(). JRA. */
|
---|
480 |
|
---|
481 | if (lp_client_schannel() && (neg_flags & NETLOGON_NEG_SCHANNEL)) {
|
---|
482 | struct rpc_pipe_client *netlogon_schannel_pipe;
|
---|
483 |
|
---|
484 | status = cli_rpc_pipe_open_schannel_with_key(
|
---|
485 | cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
|
---|
486 | DCERPC_AUTH_LEVEL_PRIVACY, domain, &pipe_hnd->dc,
|
---|
487 | &netlogon_schannel_pipe);
|
---|
488 |
|
---|
489 | if (!NT_STATUS_IS_OK(status)) {
|
---|
490 | DEBUG(0, ("Error in domain join verification (schannel setup failed): %s\n\n",
|
---|
491 | nt_errstr(status)));
|
---|
492 |
|
---|
493 | if ( NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
|
---|
494 | (sec_channel_type == SEC_CHAN_BDC) ) {
|
---|
495 | d_fprintf(stderr, _("Please make sure that no "
|
---|
496 | "computer account\nnamed "
|
---|
497 | "like this machine (%s) "
|
---|
498 | "exists in the domain\n"),
|
---|
499 | global_myname());
|
---|
500 | }
|
---|
501 |
|
---|
502 | goto done;
|
---|
503 | }
|
---|
504 | TALLOC_FREE(netlogon_schannel_pipe);
|
---|
505 | }
|
---|
506 |
|
---|
507 | TALLOC_FREE(pipe_hnd);
|
---|
508 |
|
---|
509 | /* Now store the secret in the secrets database */
|
---|
510 |
|
---|
511 | strupper_m(CONST_DISCARD(char *, domain));
|
---|
512 |
|
---|
513 | if (!secrets_store_domain_sid(domain, domain_sid)) {
|
---|
514 | DEBUG(0, ("error storing domain sid for %s\n", domain));
|
---|
515 | goto done;
|
---|
516 | }
|
---|
517 |
|
---|
518 | if (!secrets_store_machine_password(clear_trust_password, domain, sec_channel_type)) {
|
---|
519 | DEBUG(0, ("error storing plaintext domain secrets for %s\n", domain));
|
---|
520 | }
|
---|
521 |
|
---|
522 | /* double-check, connection from scratch */
|
---|
523 | status = net_rpc_join_ok(c, domain, cli->desthost, &cli->dest_ss);
|
---|
524 | retval = NT_STATUS_IS_OK(status) ? 0 : -1;
|
---|
525 |
|
---|
526 | done:
|
---|
527 |
|
---|
528 | /* Display success or failure */
|
---|
529 |
|
---|
530 | if (domain) {
|
---|
531 | if (retval != 0) {
|
---|
532 | fprintf(stderr,_("Unable to join domain %s.\n"),domain);
|
---|
533 | } else {
|
---|
534 | printf(_("Joined domain %s.\n"),domain);
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | cli_shutdown(cli);
|
---|
539 |
|
---|
540 | TALLOC_FREE(clear_trust_password);
|
---|
541 |
|
---|
542 | return retval;
|
---|
543 | }
|
---|
544 |
|
---|
545 | /**
|
---|
546 | * check that a join is OK
|
---|
547 | *
|
---|
548 | * @return A shell status integer (0 for success)
|
---|
549 | *
|
---|
550 | **/
|
---|
551 | int net_rpc_testjoin(struct net_context *c, int argc, const char **argv)
|
---|
552 | {
|
---|
553 | NTSTATUS nt_status;
|
---|
554 |
|
---|
555 | if (c->display_usage) {
|
---|
556 | d_printf(_("Usage\n"
|
---|
557 | "net rpc testjoin\n"
|
---|
558 | " Test if a join is OK\n"));
|
---|
559 | return 0;
|
---|
560 | }
|
---|
561 |
|
---|
562 | /* Display success or failure */
|
---|
563 | nt_status = net_rpc_join_ok(c, c->opt_target_workgroup, NULL, NULL);
|
---|
564 | if (!NT_STATUS_IS_OK(nt_status)) {
|
---|
565 | fprintf(stderr, _("Join to domain '%s' is not valid: %s\n"),
|
---|
566 | c->opt_target_workgroup, nt_errstr(nt_status));
|
---|
567 | return -1;
|
---|
568 | }
|
---|
569 |
|
---|
570 | printf(_("Join to '%s' is OK\n"), c->opt_target_workgroup);
|
---|
571 | return 0;
|
---|
572 | }
|
---|