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
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "utils/net.h"
23 /* Macro for checking RPC error codes to make things more readable */
25 #define CHECK_RPC_ERR(rpc, msg) \
26 if (!NT_STATUS_IS_OK(result = rpc)) { \
27 DEBUG(0, (msg ": %s\n", nt_errstr(result))); \
31 #define CHECK_RPC_ERR_DEBUG(rpc, debug_args) \
32 if (!NT_STATUS_IS_OK(result = rpc)) { \
33 DEBUG(0, debug_args); \
37 static void init_lsa_String(struct lsa_String *name, const char *s)
44 * confirm that a domain join is still valid
46 * @return A shell status integer (0 for success)
49 NTSTATUS net_rpc_join_ok(const char *domain, const char *server,
50 struct sockaddr_storage *pss)
52 enum security_types sec;
53 unsigned int conn_flags = NET_FLAGS_PDC;
54 uint32 neg_flags = NETLOGON_NEG_SELECT_AUTH2_FLAGS|NETLOGON_NEG_SCHANNEL;
55 struct cli_state *cli = NULL;
56 struct rpc_pipe_client *pipe_hnd = NULL;
57 struct rpc_pipe_client *netlogon_pipe = NULL;
58 NTSTATUS ntret = NT_STATUS_UNSUCCESSFUL;
60 sec = (enum security_types)lp_security();
63 /* Connect to IPC$ using machine account's credentials. We don't use anonymous
64 connection here, as it may be denied by server's local policy. */
65 net_use_machine_account();
68 /* some servers (e.g. WinNT) don't accept machine-authenticated
70 conn_flags |= NET_FLAGS_ANONYMOUS;
73 /* Connect to remote machine */
74 ntret = net_make_ipc_connection_ex(domain, server, pss, conn_flags, &cli);
75 if (!NT_STATUS_IS_OK(ntret)) {
79 /* Setup the creds as though we're going to do schannel... */
80 netlogon_pipe = get_schannel_session_key(cli, domain, &neg_flags, &ntret);
82 /* We return NT_STATUS_INVALID_NETWORK_RESPONSE if the server is refusing
83 to negotiate schannel, but the creds were set up ok. That'll have to do. */
86 if (NT_STATUS_EQUAL(ntret, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
90 DEBUG(0,("net_rpc_join_ok: failed to get schannel session "
91 "key from server %s for domain %s. Error was %s\n",
92 cli->desthost, domain, nt_errstr(ntret) ));
98 /* Only do the rest of the schannel test if the client is allowed to do this. */
99 if (!lp_client_schannel()) {
105 pipe_hnd = cli_rpc_pipe_open_schannel_with_key(cli, PI_NETLOGON,
106 PIPE_AUTH_LEVEL_PRIVACY,
107 domain, netlogon_pipe->dc, &ntret);
110 DEBUG(0,("net_rpc_join_ok: failed to open schannel session "
111 "on netlogon pipe to server %s for domain %s. Error was %s\n",
112 cli->desthost, domain, nt_errstr(ntret) ));
114 * Note: here, we have:
115 * (pipe_hnd != NULL) if and only if NT_STATUS_IS_OK(ntret)
124 * Join a domain using the administrator username and password
126 * @param argc Standard main() style argc
127 * @param argc Standard main() style argv. Initial components are already
128 * stripped. Currently not used.
129 * @return A shell status integer (0 for success)
133 int net_rpc_join_newstyle(int argc, const char **argv)
136 /* libsmb variables */
138 struct cli_state *cli;
140 uint32 acb_info = ACB_WSTRUST;
141 uint32 neg_flags = NETLOGON_NEG_SELECT_AUTH2_FLAGS|(lp_client_schannel() ? NETLOGON_NEG_SCHANNEL : 0);
142 uint32 sec_channel_type;
143 struct rpc_pipe_client *pipe_hnd = NULL;
147 POLICY_HND lsa_pol, sam_pol, domain_pol, user_pol;
153 char *clear_trust_password = NULL;
155 SAM_USERINFO_CTR ctr;
156 SAM_USER_INFO_24 p24;
157 SAM_USER_INFO_16 p16;
158 uchar md4_trust_password[16];
164 const char *domain = NULL;
165 uint32 num_rids, *name_types, *user_rids;
166 uint32 flags = 0x3e8;
168 const char *const_acct_name;
169 struct lsa_String lsa_acct_name;
171 uint32_t access_granted = 0;
173 /* check what type of join */
175 sec_channel_type = get_sec_channel_type(argv[0]);
177 sec_channel_type = get_sec_channel_type(NULL);
180 switch (sec_channel_type) {
182 acb_info = ACB_WSTRUST;
185 acb_info = ACB_SVRTRUST;
188 case SEC_CHAN_DOMAIN:
189 acb_info = ACB_DOMTRUST;
194 /* Make authenticated connection to remote machine */
196 result = net_make_ipc_connection(NET_FLAGS_PDC, &cli);
197 if (!NT_STATUS_IS_OK(result)) {
201 if (!(mem_ctx = talloc_init("net_rpc_join_newstyle"))) {
202 DEBUG(0, ("Could not initialise talloc context\n"));
206 /* Fetch domain sid */
208 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
210 DEBUG(0, ("Error connecting to LSA pipe. Error was %s\n",
211 nt_errstr(result) ));
216 CHECK_RPC_ERR(rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
217 SEC_RIGHTS_MAXIMUM_ALLOWED,
219 "error opening lsa policy handle");
221 CHECK_RPC_ERR(rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &lsa_pol,
222 5, &domain, &domain_sid),
223 "error querying info policy");
225 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
226 cli_rpc_pipe_close(pipe_hnd); /* Done with this pipe */
228 /* Bail out if domain didn't get set. */
230 DEBUG(0, ("Could not get domain name.\n"));
234 /* Create domain user */
235 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &result);
237 DEBUG(0, ("Error connecting to SAM pipe. Error was %s\n",
238 nt_errstr(result) ));
242 CHECK_RPC_ERR(rpccli_samr_connect(pipe_hnd, mem_ctx,
243 SEC_RIGHTS_MAXIMUM_ALLOWED,
245 "could not connect to SAM database");
248 CHECK_RPC_ERR(rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
250 SEC_RIGHTS_MAXIMUM_ALLOWED,
253 "could not open domain");
255 /* Create domain user */
256 if ((acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname())) == NULL) {
257 result = NT_STATUS_NO_MEMORY;
260 strlower_m(acct_name);
261 const_acct_name = acct_name;
263 init_lsa_String(&lsa_acct_name, acct_name);
265 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
266 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
267 SAMR_USER_ACCESS_SET_PASSWORD |
268 SAMR_USER_ACCESS_GET_ATTRIBUTES |
269 SAMR_USER_ACCESS_SET_ATTRIBUTES;
271 DEBUG(10, ("Creating account with flags: %d\n",acct_flags));
273 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
282 if (!NT_STATUS_IS_OK(result) &&
283 !NT_STATUS_EQUAL(result, NT_STATUS_USER_EXISTS)) {
284 d_fprintf(stderr, "Creation of workstation account failed\n");
286 /* If NT_STATUS_ACCESS_DENIED then we have a valid
287 username/password combo but the user does not have
288 administrator access. */
290 if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED))
291 d_fprintf(stderr, "User specified does not have administrator privileges\n");
296 /* We *must* do this.... don't ask... */
298 if (NT_STATUS_IS_OK(result)) {
299 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
302 CHECK_RPC_ERR_DEBUG(rpccli_samr_lookup_names(pipe_hnd, mem_ctx,
306 &user_rids, &name_types),
307 ("error looking up rid for user %s: %s\n",
308 acct_name, nt_errstr(result)));
310 if (name_types[0] != SID_NAME_USER) {
311 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name, name_types[0]));
315 user_rid = user_rids[0];
317 /* Open handle on user */
320 rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
322 SEC_RIGHTS_MAXIMUM_ALLOWED,
325 ("could not re-open existing user %s: %s\n",
326 acct_name, nt_errstr(result)));
328 /* Create a random machine account password */
332 str = generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
333 clear_trust_password = SMB_STRDUP(str);
334 E_md4hash(clear_trust_password, md4_trust_password);
337 encode_pw_buffer(pwbuf, clear_trust_password, STR_UNICODE);
339 /* Set password on machine account */
344 init_sam_user_info24(&p24, (char *)pwbuf,24);
346 ctr.switch_value = 24;
347 ctr.info.id24 = &p24;
349 CHECK_RPC_ERR(rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
350 &cli->user_session_key, &ctr),
351 "error setting trust account password");
353 /* Why do we have to try to (re-)set the ACB to be the same as what
354 we passed in the samr_create_dom_user() call? When a NT
355 workstation is joined to a domain by an administrator the
356 acb_info is set to 0x80. For a normal user with "Add
357 workstations to the domain" rights the acb_info is 0x84. I'm
358 not sure whether it is supposed to make a difference or not. NT
359 seems to cope with either value so don't bomb out if the set
360 userinfo2 level 0x10 fails. -tpot */
363 ctr.switch_value = 16;
364 ctr.info.id16 = &p16;
366 init_sam_user_info16(&p16, acb_info);
368 /* Ignoring the return value is necessary for joining a domain
369 as a normal user with "Add workstation to domain" privilege. */
371 result = rpccli_samr_set_userinfo2(pipe_hnd, mem_ctx, &user_pol, 16,
372 &cli->user_session_key, &ctr);
374 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
375 cli_rpc_pipe_close(pipe_hnd); /* Done with this pipe */
377 /* Now check the whole process from top-to-bottom */
379 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &result);
381 DEBUG(0,("Error connecting to NETLOGON pipe. Error was %s\n",
382 nt_errstr(result) ));
386 result = rpccli_netlogon_setup_creds(pipe_hnd,
387 cli->desthost, /* server name */
389 global_myname(), /* client name */
390 global_myname(), /* machine account name */
395 if (!NT_STATUS_IS_OK(result)) {
396 DEBUG(0, ("Error in domain join verification (credential setup failed): %s\n\n",
399 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) &&
400 (sec_channel_type == SEC_CHAN_BDC) ) {
401 d_fprintf(stderr, "Please make sure that no computer account\n"
402 "named like this machine (%s) exists in the domain\n",
409 /* We can only check the schannel connection if the client is allowed
410 to do this and the server supports it. If not, just assume success
411 (after all the rpccli_netlogon_setup_creds() succeeded, and we'll
412 do the same again (setup creds) in net_rpc_join_ok(). JRA. */
414 if (lp_client_schannel() && (neg_flags & NETLOGON_NEG_SCHANNEL)) {
415 struct rpc_pipe_client *netlogon_schannel_pipe =
416 cli_rpc_pipe_open_schannel_with_key(cli,
418 PIPE_AUTH_LEVEL_PRIVACY,
423 if (!NT_STATUS_IS_OK(result)) {
424 DEBUG(0, ("Error in domain join verification (schannel setup failed): %s\n\n",
427 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) &&
428 (sec_channel_type == SEC_CHAN_BDC) ) {
429 d_fprintf(stderr, "Please make sure that no computer account\n"
430 "named like this machine (%s) exists in the domain\n",
436 cli_rpc_pipe_close(netlogon_schannel_pipe);
439 cli_rpc_pipe_close(pipe_hnd);
441 /* Now store the secret in the secrets database */
443 strupper_m(CONST_DISCARD(char *, domain));
445 if (!secrets_store_domain_sid(domain, domain_sid)) {
446 DEBUG(0, ("error storing domain sid for %s\n", domain));
450 if (!secrets_store_machine_password(clear_trust_password, domain, sec_channel_type)) {
451 DEBUG(0, ("error storing plaintext domain secrets for %s\n", domain));
454 /* double-check, connection from scratch */
455 result = net_rpc_join_ok(domain, cli->desthost, &cli->dest_ss);
456 retval = NT_STATUS_IS_OK(result) ? 0 : -1;
460 /* Display success or failure */
464 fprintf(stderr,"Unable to join domain %s.\n",domain);
466 printf("Joined domain %s.\n",domain);
472 SAFE_FREE(clear_trust_password);
478 * check that a join is OK
480 * @return A shell status integer (0 for success)
483 int net_rpc_testjoin(int argc, const char **argv)
485 char *domain = smb_xstrdup(opt_target_workgroup);
488 /* Display success or failure */
489 nt_status = net_rpc_join_ok(domain, NULL, NULL);
490 if (!NT_STATUS_IS_OK(nt_status)) {
491 fprintf(stderr,"Join to domain '%s' is not valid: %s\n",
492 nt_errstr(nt_status), domain);
497 printf("Join to '%s' is OK\n",domain);