s3: Fix an uninitialized variable
[Samba/gebeck_regimport.git] / source3 / utils / net_rpc_join.c
blob4659fe52ec8f606006dab89e21d3664bfabfdd1d
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
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/>. */
21 #include "includes.h"
22 #include "utils/net.h"
23 #include "../libcli/auth/libcli_auth.h"
24 #include "../librpc/gen_ndr/cli_lsa.h"
25 #include "rpc_client/cli_lsarpc.h"
26 #include "../librpc/gen_ndr/cli_samr.h"
27 #include "rpc_client/init_samr.h"
28 #include "../librpc/gen_ndr/ndr_netlogon.h"
29 #include "rpc_client/cli_netlogon.h"
31 /* Macro for checking RPC error codes to make things more readable */
33 #define CHECK_RPC_ERR(rpc, msg) \
34 if (!NT_STATUS_IS_OK(result = rpc)) { \
35 DEBUG(0, (msg ": %s\n", nt_errstr(result))); \
36 goto done; \
39 #define CHECK_RPC_ERR_DEBUG(rpc, debug_args) \
40 if (!NT_STATUS_IS_OK(result = rpc)) { \
41 DEBUG(0, debug_args); \
42 goto done; \
45 /**
46 * confirm that a domain join is still valid
48 * @return A shell status integer (0 for success)
50 **/
51 NTSTATUS net_rpc_join_ok(struct net_context *c, const char *domain,
52 const char *server, struct sockaddr_storage *pss)
54 enum security_types sec;
55 unsigned int conn_flags = NET_FLAGS_PDC;
56 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
57 struct cli_state *cli = NULL;
58 struct rpc_pipe_client *pipe_hnd = NULL;
59 struct rpc_pipe_client *netlogon_pipe = NULL;
60 NTSTATUS ntret = NT_STATUS_UNSUCCESSFUL;
62 sec = (enum security_types)lp_security();
64 if (sec == SEC_ADS) {
65 /* Connect to IPC$ using machine account's credentials. We don't use anonymous
66 connection here, as it may be denied by server's local policy. */
67 net_use_machine_account(c);
69 } else {
70 /* some servers (e.g. WinNT) don't accept machine-authenticated
71 smb connections */
72 conn_flags |= NET_FLAGS_ANONYMOUS;
75 /* Connect to remote machine */
76 ntret = net_make_ipc_connection_ex(c, domain, server, pss, conn_flags,
77 &cli);
78 if (!NT_STATUS_IS_OK(ntret)) {
79 return ntret;
82 /* Setup the creds as though we're going to do schannel... */
83 ntret = get_schannel_session_key(cli, domain, &neg_flags,
84 &netlogon_pipe);
86 /* We return NT_STATUS_INVALID_NETWORK_RESPONSE if the server is refusing
87 to negotiate schannel, but the creds were set up ok. That'll have to do. */
89 if (!NT_STATUS_IS_OK(ntret)) {
90 if (NT_STATUS_EQUAL(ntret, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
91 cli_shutdown(cli);
92 return NT_STATUS_OK;
93 } else {
94 DEBUG(0,("net_rpc_join_ok: failed to get schannel session "
95 "key from server %s for domain %s. Error was %s\n",
96 cli->desthost, domain, nt_errstr(ntret) ));
97 cli_shutdown(cli);
98 return ntret;
102 /* Only do the rest of the schannel test if the client is allowed to do this. */
103 if (!lp_client_schannel()) {
104 cli_shutdown(cli);
105 /* We're good... */
106 return ntret;
109 ntret = cli_rpc_pipe_open_schannel_with_key(
110 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
111 DCERPC_AUTH_LEVEL_PRIVACY,
112 domain, &netlogon_pipe->dc, &pipe_hnd);
114 if (!NT_STATUS_IS_OK(ntret)) {
115 DEBUG(0,("net_rpc_join_ok: failed to open schannel session "
116 "on netlogon pipe to server %s for domain %s. Error was %s\n",
117 cli->desthost, domain, nt_errstr(ntret) ));
119 * Note: here, we have:
120 * (pipe_hnd != NULL) if and only if NT_STATUS_IS_OK(ntret)
124 cli_shutdown(cli);
125 return ntret;
129 * Join a domain using the administrator username and password
131 * @param argc Standard main() style argc
132 * @param argc Standard main() style argv. Initial components are already
133 * stripped. Currently not used.
134 * @return A shell status integer (0 for success)
138 int net_rpc_join_newstyle(struct net_context *c, int argc, const char **argv)
141 /* libsmb variables */
143 struct cli_state *cli;
144 TALLOC_CTX *mem_ctx;
145 uint32 acb_info = ACB_WSTRUST;
146 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
147 enum netr_SchannelType sec_channel_type;
148 struct rpc_pipe_client *pipe_hnd = NULL;
150 /* rpc variables */
152 struct policy_handle lsa_pol, sam_pol, domain_pol, user_pol;
153 struct dom_sid *domain_sid;
154 uint32 user_rid;
156 /* Password stuff */
158 char *clear_trust_password = NULL;
159 struct samr_CryptPassword crypt_pwd;
160 uchar md4_trust_password[16];
161 union samr_UserInfo set_info;
163 /* Misc */
165 NTSTATUS result;
166 int retval = 1;
167 const char *domain = NULL;
168 char *acct_name;
169 struct lsa_String lsa_acct_name;
170 uint32 acct_flags=0;
171 uint32_t access_granted = 0;
172 union lsa_PolicyInformation *info = NULL;
173 struct samr_Ids user_rids;
174 struct samr_Ids name_types;
176 /* check what type of join */
177 if (argc >= 0) {
178 sec_channel_type = get_sec_channel_type(argv[0]);
179 } else {
180 sec_channel_type = get_sec_channel_type(NULL);
183 switch (sec_channel_type) {
184 case SEC_CHAN_WKSTA:
185 acb_info = ACB_WSTRUST;
186 break;
187 case SEC_CHAN_BDC:
188 acb_info = ACB_SVRTRUST;
189 break;
190 #if 0
191 case SEC_CHAN_DOMAIN:
192 acb_info = ACB_DOMTRUST;
193 break;
194 #endif
195 default:
196 DEBUG(0,("secure channel type %d not yet supported\n",
197 sec_channel_type));
198 break;
201 /* Make authenticated connection to remote machine */
203 result = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
204 if (!NT_STATUS_IS_OK(result)) {
205 return 1;
208 if (!(mem_ctx = talloc_init("net_rpc_join_newstyle"))) {
209 DEBUG(0, ("Could not initialise talloc context\n"));
210 goto done;
213 /* Fetch domain sid */
215 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
216 &pipe_hnd);
217 if (!NT_STATUS_IS_OK(result)) {
218 DEBUG(0, ("Error connecting to LSA pipe. Error was %s\n",
219 nt_errstr(result) ));
220 goto done;
224 CHECK_RPC_ERR(rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
225 SEC_FLAG_MAXIMUM_ALLOWED,
226 &lsa_pol),
227 "error opening lsa policy handle");
229 CHECK_RPC_ERR(rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
230 &lsa_pol,
231 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
232 &info),
233 "error querying info policy");
235 domain = info->account_domain.name.string;
236 domain_sid = info->account_domain.sid;
238 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
239 TALLOC_FREE(pipe_hnd); /* Done with this pipe */
241 /* Bail out if domain didn't get set. */
242 if (!domain) {
243 DEBUG(0, ("Could not get domain name.\n"));
244 goto done;
247 /* Create domain user */
248 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
249 &pipe_hnd);
250 if (!NT_STATUS_IS_OK(result)) {
251 DEBUG(0, ("Error connecting to SAM pipe. Error was %s\n",
252 nt_errstr(result) ));
253 goto done;
256 CHECK_RPC_ERR(rpccli_samr_Connect2(pipe_hnd, mem_ctx,
257 pipe_hnd->desthost,
258 SAMR_ACCESS_ENUM_DOMAINS
259 | SAMR_ACCESS_LOOKUP_DOMAIN,
260 &sam_pol),
261 "could not connect to SAM database");
264 CHECK_RPC_ERR(rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
265 &sam_pol,
266 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
267 | SAMR_DOMAIN_ACCESS_CREATE_USER
268 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
269 domain_sid,
270 &domain_pol),
271 "could not open domain");
273 /* Create domain user */
274 if ((acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname())) == NULL) {
275 result = NT_STATUS_NO_MEMORY;
276 goto done;
278 strlower_m(acct_name);
280 init_lsa_String(&lsa_acct_name, acct_name);
282 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
283 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
284 SAMR_USER_ACCESS_SET_PASSWORD |
285 SAMR_USER_ACCESS_GET_ATTRIBUTES |
286 SAMR_USER_ACCESS_SET_ATTRIBUTES;
288 DEBUG(10, ("Creating account with flags: %d\n",acct_flags));
290 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
291 &domain_pol,
292 &lsa_acct_name,
293 acb_info,
294 acct_flags,
295 &user_pol,
296 &access_granted,
297 &user_rid);
299 if (!NT_STATUS_IS_OK(result) &&
300 !NT_STATUS_EQUAL(result, NT_STATUS_USER_EXISTS)) {
301 d_fprintf(stderr,_("Creation of workstation account failed\n"));
303 /* If NT_STATUS_ACCESS_DENIED then we have a valid
304 username/password combo but the user does not have
305 administrator access. */
307 if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED))
308 d_fprintf(stderr, _("User specified does not have "
309 "administrator privileges\n"));
311 goto done;
314 /* We *must* do this.... don't ask... */
316 if (NT_STATUS_IS_OK(result)) {
317 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
320 CHECK_RPC_ERR_DEBUG(rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
321 &domain_pol,
323 &lsa_acct_name,
324 &user_rids,
325 &name_types),
326 ("error looking up rid for user %s: %s\n",
327 acct_name, nt_errstr(result)));
329 if (name_types.ids[0] != SID_NAME_USER) {
330 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name, name_types.ids[0]));
331 goto done;
334 user_rid = user_rids.ids[0];
336 /* Open handle on user */
338 CHECK_RPC_ERR_DEBUG(
339 rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
340 &domain_pol,
341 SEC_FLAG_MAXIMUM_ALLOWED,
342 user_rid,
343 &user_pol),
344 ("could not re-open existing user %s: %s\n",
345 acct_name, nt_errstr(result)));
347 /* Create a random machine account password */
349 clear_trust_password = generate_random_str(talloc_tos(), DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
350 E_md4hash(clear_trust_password, md4_trust_password);
352 /* Set password on machine account */
354 init_samr_CryptPassword(clear_trust_password,
355 &cli->user_session_key,
356 &crypt_pwd);
358 set_info.info24.password = crypt_pwd;
359 set_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
361 CHECK_RPC_ERR(rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
362 &user_pol,
364 &set_info),
365 "error setting trust account password");
367 /* Why do we have to try to (re-)set the ACB to be the same as what
368 we passed in the samr_create_dom_user() call? When a NT
369 workstation is joined to a domain by an administrator the
370 acb_info is set to 0x80. For a normal user with "Add
371 workstations to the domain" rights the acb_info is 0x84. I'm
372 not sure whether it is supposed to make a difference or not. NT
373 seems to cope with either value so don't bomb out if the set
374 userinfo2 level 0x10 fails. -tpot */
376 set_info.info16.acct_flags = acb_info;
378 /* Ignoring the return value is necessary for joining a domain
379 as a normal user with "Add workstation to domain" privilege. */
381 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
382 &user_pol,
384 &set_info);
386 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
387 TALLOC_FREE(pipe_hnd); /* Done with this pipe */
389 /* Now check the whole process from top-to-bottom */
391 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
392 &pipe_hnd);
393 if (!NT_STATUS_IS_OK(result)) {
394 DEBUG(0,("Error connecting to NETLOGON pipe. Error was %s\n",
395 nt_errstr(result) ));
396 goto done;
399 result = rpccli_netlogon_setup_creds(pipe_hnd,
400 cli->desthost, /* server name */
401 domain, /* domain */
402 global_myname(), /* client name */
403 global_myname(), /* machine account name */
404 md4_trust_password,
405 sec_channel_type,
406 &neg_flags);
408 if (!NT_STATUS_IS_OK(result)) {
409 DEBUG(0, ("Error in domain join verification (credential setup failed): %s\n\n",
410 nt_errstr(result)));
412 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) &&
413 (sec_channel_type == SEC_CHAN_BDC) ) {
414 d_fprintf(stderr, _("Please make sure that no computer "
415 "account\nnamed like this machine "
416 "(%s) exists in the domain\n"),
417 global_myname());
420 goto done;
423 /* We can only check the schannel connection if the client is allowed
424 to do this and the server supports it. If not, just assume success
425 (after all the rpccli_netlogon_setup_creds() succeeded, and we'll
426 do the same again (setup creds) in net_rpc_join_ok(). JRA. */
428 if (lp_client_schannel() && (neg_flags & NETLOGON_NEG_SCHANNEL)) {
429 struct rpc_pipe_client *netlogon_schannel_pipe;
431 result = cli_rpc_pipe_open_schannel_with_key(
432 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
433 DCERPC_AUTH_LEVEL_PRIVACY, domain, &pipe_hnd->dc,
434 &netlogon_schannel_pipe);
436 if (!NT_STATUS_IS_OK(result)) {
437 DEBUG(0, ("Error in domain join verification (schannel setup failed): %s\n\n",
438 nt_errstr(result)));
440 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) &&
441 (sec_channel_type == SEC_CHAN_BDC) ) {
442 d_fprintf(stderr, _("Please make sure that no "
443 "computer account\nnamed "
444 "like this machine (%s) "
445 "exists in the domain\n"),
446 global_myname());
449 goto done;
451 TALLOC_FREE(netlogon_schannel_pipe);
454 TALLOC_FREE(pipe_hnd);
456 /* Now store the secret in the secrets database */
458 strupper_m(CONST_DISCARD(char *, domain));
460 if (!secrets_store_domain_sid(domain, domain_sid)) {
461 DEBUG(0, ("error storing domain sid for %s\n", domain));
462 goto done;
465 if (!secrets_store_machine_password(clear_trust_password, domain, sec_channel_type)) {
466 DEBUG(0, ("error storing plaintext domain secrets for %s\n", domain));
469 /* double-check, connection from scratch */
470 result = net_rpc_join_ok(c, domain, cli->desthost, &cli->dest_ss);
471 retval = NT_STATUS_IS_OK(result) ? 0 : -1;
473 done:
475 /* Display success or failure */
477 if (domain) {
478 if (retval != 0) {
479 fprintf(stderr,_("Unable to join domain %s.\n"),domain);
480 } else {
481 printf(_("Joined domain %s.\n"),domain);
485 cli_shutdown(cli);
487 TALLOC_FREE(clear_trust_password);
489 return retval;
493 * check that a join is OK
495 * @return A shell status integer (0 for success)
498 int net_rpc_testjoin(struct net_context *c, int argc, const char **argv)
500 NTSTATUS nt_status;
502 if (c->display_usage) {
503 d_printf(_("Usage\n"
504 "net rpc testjoin\n"
505 " Test if a join is OK\n"));
506 return 0;
509 /* Display success or failure */
510 nt_status = net_rpc_join_ok(c, c->opt_target_workgroup, NULL, NULL);
511 if (!NT_STATUS_IS_OK(nt_status)) {
512 fprintf(stderr, _("Join to domain '%s' is not valid: %s\n"),
513 c->opt_target_workgroup, nt_errstr(nt_status));
514 return -1;
517 printf(_("Join to '%s' is OK\n"), c->opt_target_workgroup);
518 return 0;