s3-dcerpc: Pull packet in the caller, before validation
[Samba.git] / source3 / utils / net_rpc_join.c
blobc17dfa4c3264db12c003d672f3861430410496d2
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"
30 #include "secrets.h"
32 /* Macro for checking RPC error codes to make things more readable */
34 #define CHECK_RPC_ERR(rpc, msg) \
35 if (!NT_STATUS_IS_OK(result = rpc)) { \
36 DEBUG(0, (msg ": %s\n", nt_errstr(result))); \
37 goto done; \
40 #define CHECK_RPC_ERR_DEBUG(rpc, debug_args) \
41 if (!NT_STATUS_IS_OK(result = rpc)) { \
42 DEBUG(0, debug_args); \
43 goto done; \
46 /**
47 * confirm that a domain join is still valid
49 * @return A shell status integer (0 for success)
51 **/
52 NTSTATUS net_rpc_join_ok(struct net_context *c, const char *domain,
53 const char *server, struct sockaddr_storage *pss)
55 enum security_types sec;
56 unsigned int conn_flags = NET_FLAGS_PDC;
57 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
58 struct cli_state *cli = NULL;
59 struct rpc_pipe_client *pipe_hnd = NULL;
60 struct rpc_pipe_client *netlogon_pipe = NULL;
61 NTSTATUS ntret = NT_STATUS_UNSUCCESSFUL;
63 sec = (enum security_types)lp_security();
65 if (sec == SEC_ADS) {
66 /* Connect to IPC$ using machine account's credentials. We don't use anonymous
67 connection here, as it may be denied by server's local policy. */
68 net_use_machine_account(c);
70 } else {
71 /* some servers (e.g. WinNT) don't accept machine-authenticated
72 smb connections */
73 conn_flags |= NET_FLAGS_ANONYMOUS;
76 /* Connect to remote machine */
77 ntret = net_make_ipc_connection_ex(c, domain, server, pss, conn_flags,
78 &cli);
79 if (!NT_STATUS_IS_OK(ntret)) {
80 return ntret;
83 /* Setup the creds as though we're going to do schannel... */
84 ntret = get_schannel_session_key(cli, domain, &neg_flags,
85 &netlogon_pipe);
87 /* We return NT_STATUS_INVALID_NETWORK_RESPONSE if the server is refusing
88 to negotiate schannel, but the creds were set up ok. That'll have to do. */
90 if (!NT_STATUS_IS_OK(ntret)) {
91 if (NT_STATUS_EQUAL(ntret, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
92 cli_shutdown(cli);
93 return NT_STATUS_OK;
94 } else {
95 DEBUG(0,("net_rpc_join_ok: failed to get schannel session "
96 "key from server %s for domain %s. Error was %s\n",
97 cli->desthost, domain, nt_errstr(ntret) ));
98 cli_shutdown(cli);
99 return ntret;
103 /* Only do the rest of the schannel test if the client is allowed to do this. */
104 if (!lp_client_schannel()) {
105 cli_shutdown(cli);
106 /* We're good... */
107 return ntret;
110 ntret = cli_rpc_pipe_open_schannel_with_key(
111 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
112 DCERPC_AUTH_LEVEL_PRIVACY,
113 domain, &netlogon_pipe->dc, &pipe_hnd);
115 if (!NT_STATUS_IS_OK(ntret)) {
116 DEBUG(0,("net_rpc_join_ok: failed to open schannel session "
117 "on netlogon pipe to server %s for domain %s. Error was %s\n",
118 cli->desthost, domain, nt_errstr(ntret) ));
120 * Note: here, we have:
121 * (pipe_hnd != NULL) if and only if NT_STATUS_IS_OK(ntret)
125 cli_shutdown(cli);
126 return ntret;
130 * Join a domain using the administrator username and password
132 * @param argc Standard main() style argc
133 * @param argc Standard main() style argv. Initial components are already
134 * stripped. Currently not used.
135 * @return A shell status integer (0 for success)
139 int net_rpc_join_newstyle(struct net_context *c, int argc, const char **argv)
142 /* libsmb variables */
144 struct cli_state *cli;
145 TALLOC_CTX *mem_ctx;
146 uint32 acb_info = ACB_WSTRUST;
147 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
148 enum netr_SchannelType sec_channel_type;
149 struct rpc_pipe_client *pipe_hnd = NULL;
151 /* rpc variables */
153 struct policy_handle lsa_pol, sam_pol, domain_pol, user_pol;
154 struct dom_sid *domain_sid;
155 uint32 user_rid;
157 /* Password stuff */
159 char *clear_trust_password = NULL;
160 struct samr_CryptPassword crypt_pwd;
161 uchar md4_trust_password[16];
162 union samr_UserInfo set_info;
164 /* Misc */
166 NTSTATUS result;
167 int retval = 1;
168 const char *domain = NULL;
169 char *acct_name;
170 struct lsa_String lsa_acct_name;
171 uint32 acct_flags=0;
172 uint32_t access_granted = 0;
173 union lsa_PolicyInformation *info = NULL;
174 struct samr_Ids user_rids;
175 struct samr_Ids name_types;
177 /* check what type of join */
178 if (argc >= 0) {
179 sec_channel_type = get_sec_channel_type(argv[0]);
180 } else {
181 sec_channel_type = get_sec_channel_type(NULL);
184 switch (sec_channel_type) {
185 case SEC_CHAN_WKSTA:
186 acb_info = ACB_WSTRUST;
187 break;
188 case SEC_CHAN_BDC:
189 acb_info = ACB_SVRTRUST;
190 break;
191 #if 0
192 case SEC_CHAN_DOMAIN:
193 acb_info = ACB_DOMTRUST;
194 break;
195 #endif
196 default:
197 DEBUG(0,("secure channel type %d not yet supported\n",
198 sec_channel_type));
199 break;
202 /* Make authenticated connection to remote machine */
204 result = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
205 if (!NT_STATUS_IS_OK(result)) {
206 return 1;
209 if (!(mem_ctx = talloc_init("net_rpc_join_newstyle"))) {
210 DEBUG(0, ("Could not initialise talloc context\n"));
211 goto done;
214 /* Fetch domain sid */
216 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
217 &pipe_hnd);
218 if (!NT_STATUS_IS_OK(result)) {
219 DEBUG(0, ("Error connecting to LSA pipe. Error was %s\n",
220 nt_errstr(result) ));
221 goto done;
225 CHECK_RPC_ERR(rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
226 SEC_FLAG_MAXIMUM_ALLOWED,
227 &lsa_pol),
228 "error opening lsa policy handle");
230 CHECK_RPC_ERR(rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
231 &lsa_pol,
232 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
233 &info),
234 "error querying info policy");
236 domain = info->account_domain.name.string;
237 domain_sid = info->account_domain.sid;
239 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
240 TALLOC_FREE(pipe_hnd); /* Done with this pipe */
242 /* Bail out if domain didn't get set. */
243 if (!domain) {
244 DEBUG(0, ("Could not get domain name.\n"));
245 goto done;
248 /* Create domain user */
249 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
250 &pipe_hnd);
251 if (!NT_STATUS_IS_OK(result)) {
252 DEBUG(0, ("Error connecting to SAM pipe. Error was %s\n",
253 nt_errstr(result) ));
254 goto done;
257 CHECK_RPC_ERR(rpccli_samr_Connect2(pipe_hnd, mem_ctx,
258 pipe_hnd->desthost,
259 SAMR_ACCESS_ENUM_DOMAINS
260 | SAMR_ACCESS_LOOKUP_DOMAIN,
261 &sam_pol),
262 "could not connect to SAM database");
265 CHECK_RPC_ERR(rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
266 &sam_pol,
267 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
268 | SAMR_DOMAIN_ACCESS_CREATE_USER
269 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
270 domain_sid,
271 &domain_pol),
272 "could not open domain");
274 /* Create domain user */
275 if ((acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname())) == NULL) {
276 result = NT_STATUS_NO_MEMORY;
277 goto done;
279 strlower_m(acct_name);
281 init_lsa_String(&lsa_acct_name, acct_name);
283 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
284 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
285 SAMR_USER_ACCESS_SET_PASSWORD |
286 SAMR_USER_ACCESS_GET_ATTRIBUTES |
287 SAMR_USER_ACCESS_SET_ATTRIBUTES;
289 DEBUG(10, ("Creating account with flags: %d\n",acct_flags));
291 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
292 &domain_pol,
293 &lsa_acct_name,
294 acb_info,
295 acct_flags,
296 &user_pol,
297 &access_granted,
298 &user_rid);
300 if (!NT_STATUS_IS_OK(result) &&
301 !NT_STATUS_EQUAL(result, NT_STATUS_USER_EXISTS)) {
302 d_fprintf(stderr,_("Creation of workstation account failed\n"));
304 /* If NT_STATUS_ACCESS_DENIED then we have a valid
305 username/password combo but the user does not have
306 administrator access. */
308 if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED))
309 d_fprintf(stderr, _("User specified does not have "
310 "administrator privileges\n"));
312 goto done;
315 /* We *must* do this.... don't ask... */
317 if (NT_STATUS_IS_OK(result)) {
318 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
321 CHECK_RPC_ERR_DEBUG(rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
322 &domain_pol,
324 &lsa_acct_name,
325 &user_rids,
326 &name_types),
327 ("error looking up rid for user %s: %s\n",
328 acct_name, nt_errstr(result)));
330 if (name_types.ids[0] != SID_NAME_USER) {
331 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name, name_types.ids[0]));
332 goto done;
335 user_rid = user_rids.ids[0];
337 /* Open handle on user */
339 CHECK_RPC_ERR_DEBUG(
340 rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
341 &domain_pol,
342 SEC_FLAG_MAXIMUM_ALLOWED,
343 user_rid,
344 &user_pol),
345 ("could not re-open existing user %s: %s\n",
346 acct_name, nt_errstr(result)));
348 /* Create a random machine account password */
350 clear_trust_password = generate_random_str(talloc_tos(), DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
351 E_md4hash(clear_trust_password, md4_trust_password);
353 /* Set password on machine account */
355 init_samr_CryptPassword(clear_trust_password,
356 &cli->user_session_key,
357 &crypt_pwd);
359 set_info.info24.password = crypt_pwd;
360 set_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
362 CHECK_RPC_ERR(rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
363 &user_pol,
365 &set_info),
366 "error setting trust account password");
368 /* Why do we have to try to (re-)set the ACB to be the same as what
369 we passed in the samr_create_dom_user() call? When a NT
370 workstation is joined to a domain by an administrator the
371 acb_info is set to 0x80. For a normal user with "Add
372 workstations to the domain" rights the acb_info is 0x84. I'm
373 not sure whether it is supposed to make a difference or not. NT
374 seems to cope with either value so don't bomb out if the set
375 userinfo2 level 0x10 fails. -tpot */
377 set_info.info16.acct_flags = acb_info;
379 /* Ignoring the return value is necessary for joining a domain
380 as a normal user with "Add workstation to domain" privilege. */
382 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
383 &user_pol,
385 &set_info);
387 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
388 TALLOC_FREE(pipe_hnd); /* Done with this pipe */
390 /* Now check the whole process from top-to-bottom */
392 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
393 &pipe_hnd);
394 if (!NT_STATUS_IS_OK(result)) {
395 DEBUG(0,("Error connecting to NETLOGON pipe. Error was %s\n",
396 nt_errstr(result) ));
397 goto done;
400 result = rpccli_netlogon_setup_creds(pipe_hnd,
401 cli->desthost, /* server name */
402 domain, /* domain */
403 global_myname(), /* client name */
404 global_myname(), /* machine account name */
405 md4_trust_password,
406 sec_channel_type,
407 &neg_flags);
409 if (!NT_STATUS_IS_OK(result)) {
410 DEBUG(0, ("Error in domain join verification (credential setup failed): %s\n\n",
411 nt_errstr(result)));
413 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) &&
414 (sec_channel_type == SEC_CHAN_BDC) ) {
415 d_fprintf(stderr, _("Please make sure that no computer "
416 "account\nnamed like this machine "
417 "(%s) exists in the domain\n"),
418 global_myname());
421 goto done;
424 /* We can only check the schannel connection if the client is allowed
425 to do this and the server supports it. If not, just assume success
426 (after all the rpccli_netlogon_setup_creds() succeeded, and we'll
427 do the same again (setup creds) in net_rpc_join_ok(). JRA. */
429 if (lp_client_schannel() && (neg_flags & NETLOGON_NEG_SCHANNEL)) {
430 struct rpc_pipe_client *netlogon_schannel_pipe;
432 result = cli_rpc_pipe_open_schannel_with_key(
433 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
434 DCERPC_AUTH_LEVEL_PRIVACY, domain, &pipe_hnd->dc,
435 &netlogon_schannel_pipe);
437 if (!NT_STATUS_IS_OK(result)) {
438 DEBUG(0, ("Error in domain join verification (schannel setup failed): %s\n\n",
439 nt_errstr(result)));
441 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) &&
442 (sec_channel_type == SEC_CHAN_BDC) ) {
443 d_fprintf(stderr, _("Please make sure that no "
444 "computer account\nnamed "
445 "like this machine (%s) "
446 "exists in the domain\n"),
447 global_myname());
450 goto done;
452 TALLOC_FREE(netlogon_schannel_pipe);
455 TALLOC_FREE(pipe_hnd);
457 /* Now store the secret in the secrets database */
459 strupper_m(CONST_DISCARD(char *, domain));
461 if (!secrets_store_domain_sid(domain, domain_sid)) {
462 DEBUG(0, ("error storing domain sid for %s\n", domain));
463 goto done;
466 if (!secrets_store_machine_password(clear_trust_password, domain, sec_channel_type)) {
467 DEBUG(0, ("error storing plaintext domain secrets for %s\n", domain));
470 /* double-check, connection from scratch */
471 result = net_rpc_join_ok(c, domain, cli->desthost, &cli->dest_ss);
472 retval = NT_STATUS_IS_OK(result) ? 0 : -1;
474 done:
476 /* Display success or failure */
478 if (domain) {
479 if (retval != 0) {
480 fprintf(stderr,_("Unable to join domain %s.\n"),domain);
481 } else {
482 printf(_("Joined domain %s.\n"),domain);
486 cli_shutdown(cli);
488 TALLOC_FREE(clear_trust_password);
490 return retval;
494 * check that a join is OK
496 * @return A shell status integer (0 for success)
499 int net_rpc_testjoin(struct net_context *c, int argc, const char **argv)
501 NTSTATUS nt_status;
503 if (c->display_usage) {
504 d_printf(_("Usage\n"
505 "net rpc testjoin\n"
506 " Test if a join is OK\n"));
507 return 0;
510 /* Display success or failure */
511 nt_status = net_rpc_join_ok(c, c->opt_target_workgroup, NULL, NULL);
512 if (!NT_STATUS_IS_OK(nt_status)) {
513 fprintf(stderr, _("Join to domain '%s' is not valid: %s\n"),
514 c->opt_target_workgroup, nt_errstr(nt_status));
515 return -1;
518 printf(_("Join to '%s' is OK\n"), c->opt_target_workgroup);
519 return 0;