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"
25 /* Macro for checking RPC error codes to make things more readable */
27 #define CHECK_RPC_ERR(rpc, msg) \
28 if (!NT_STATUS_IS_OK(result = rpc)) { \
29 DEBUG(0, (msg ": %s\n", nt_errstr(result))); \
33 #define CHECK_RPC_ERR_DEBUG(rpc, debug_args) \
34 if (!NT_STATUS_IS_OK(result = rpc)) { \
35 DEBUG(0, debug_args); \
40 * confirm that a domain join is still valid
42 * @return A shell status integer (0 for success)
45 NTSTATUS
net_rpc_join_ok(struct net_context
*c
, const char *domain
,
46 const char *server
, struct sockaddr_storage
*pss
)
48 enum security_types sec
;
49 unsigned int conn_flags
= NET_FLAGS_PDC
;
50 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
51 struct cli_state
*cli
= NULL
;
52 struct rpc_pipe_client
*pipe_hnd
= NULL
;
53 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
54 NTSTATUS ntret
= NT_STATUS_UNSUCCESSFUL
;
56 sec
= (enum security_types
)lp_security();
59 /* Connect to IPC$ using machine account's credentials. We don't use anonymous
60 connection here, as it may be denied by server's local policy. */
61 net_use_machine_account(c
);
64 /* some servers (e.g. WinNT) don't accept machine-authenticated
66 conn_flags
|= NET_FLAGS_ANONYMOUS
;
69 /* Connect to remote machine */
70 ntret
= net_make_ipc_connection_ex(c
, domain
, server
, pss
, conn_flags
,
72 if (!NT_STATUS_IS_OK(ntret
)) {
76 /* Setup the creds as though we're going to do schannel... */
77 ntret
= get_schannel_session_key(cli
, domain
, &neg_flags
,
80 /* We return NT_STATUS_INVALID_NETWORK_RESPONSE if the server is refusing
81 to negotiate schannel, but the creds were set up ok. That'll have to do. */
83 if (!NT_STATUS_IS_OK(ntret
)) {
84 if (NT_STATUS_EQUAL(ntret
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
88 DEBUG(0,("net_rpc_join_ok: failed to get schannel session "
89 "key from server %s for domain %s. Error was %s\n",
90 cli
->desthost
, domain
, nt_errstr(ntret
) ));
96 /* Only do the rest of the schannel test if the client is allowed to do this. */
97 if (!lp_client_schannel()) {
103 ntret
= cli_rpc_pipe_open_schannel_with_key(
104 cli
, &ndr_table_netlogon
.syntax_id
, PIPE_AUTH_LEVEL_PRIVACY
,
105 domain
, &netlogon_pipe
->dc
, &pipe_hnd
);
107 if (!NT_STATUS_IS_OK(ntret
)) {
108 DEBUG(0,("net_rpc_join_ok: failed to open schannel session "
109 "on netlogon pipe to server %s for domain %s. Error was %s\n",
110 cli
->desthost
, domain
, nt_errstr(ntret
) ));
112 * Note: here, we have:
113 * (pipe_hnd != NULL) if and only if NT_STATUS_IS_OK(ntret)
122 * Join a domain using the administrator username and password
124 * @param argc Standard main() style argc
125 * @param argc Standard main() style argv. Initial components are already
126 * stripped. Currently not used.
127 * @return A shell status integer (0 for success)
131 int net_rpc_join_newstyle(struct net_context
*c
, int argc
, const char **argv
)
134 /* libsmb variables */
136 struct cli_state
*cli
;
138 uint32 acb_info
= ACB_WSTRUST
;
139 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
140 uint32 sec_channel_type
;
141 struct rpc_pipe_client
*pipe_hnd
= NULL
;
145 struct policy_handle lsa_pol
, sam_pol
, domain_pol
, user_pol
;
151 char *clear_trust_password
= NULL
;
152 struct samr_CryptPassword crypt_pwd
;
153 uchar md4_trust_password
[16];
154 union samr_UserInfo set_info
;
160 const char *domain
= NULL
;
162 struct lsa_String lsa_acct_name
;
164 uint32_t access_granted
= 0;
165 union lsa_PolicyInformation
*info
= NULL
;
166 struct samr_Ids user_rids
;
167 struct samr_Ids name_types
;
169 /* check what type of join */
171 sec_channel_type
= get_sec_channel_type(argv
[0]);
173 sec_channel_type
= get_sec_channel_type(NULL
);
176 switch (sec_channel_type
) {
178 acb_info
= ACB_WSTRUST
;
181 acb_info
= ACB_SVRTRUST
;
184 case SEC_CHAN_DOMAIN
:
185 acb_info
= ACB_DOMTRUST
;
190 /* Make authenticated connection to remote machine */
192 result
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
193 if (!NT_STATUS_IS_OK(result
)) {
197 if (!(mem_ctx
= talloc_init("net_rpc_join_newstyle"))) {
198 DEBUG(0, ("Could not initialise talloc context\n"));
202 /* Fetch domain sid */
204 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
206 if (!NT_STATUS_IS_OK(result
)) {
207 DEBUG(0, ("Error connecting to LSA pipe. Error was %s\n",
208 nt_errstr(result
) ));
213 CHECK_RPC_ERR(rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
214 SEC_RIGHTS_MAXIMUM_ALLOWED
,
216 "error opening lsa policy handle");
218 CHECK_RPC_ERR(rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
220 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
222 "error querying info policy");
224 domain
= info
->account_domain
.name
.string
;
225 domain_sid
= info
->account_domain
.sid
;
227 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
228 TALLOC_FREE(pipe_hnd
); /* Done with this pipe */
230 /* Bail out if domain didn't get set. */
232 DEBUG(0, ("Could not get domain name.\n"));
236 /* Create domain user */
237 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
239 if (!NT_STATUS_IS_OK(result
)) {
240 DEBUG(0, ("Error connecting to SAM pipe. Error was %s\n",
241 nt_errstr(result
) ));
245 CHECK_RPC_ERR(rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
247 SAMR_ACCESS_ENUM_DOMAINS
248 | SAMR_ACCESS_LOOKUP_DOMAIN
,
250 "could not connect to SAM database");
253 CHECK_RPC_ERR(rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
255 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
256 | SAMR_DOMAIN_ACCESS_CREATE_USER
257 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
260 "could not open domain");
262 /* Create domain user */
263 if ((acct_name
= talloc_asprintf(mem_ctx
, "%s$", global_myname())) == NULL
) {
264 result
= NT_STATUS_NO_MEMORY
;
267 strlower_m(acct_name
);
269 init_lsa_String(&lsa_acct_name
, acct_name
);
271 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
272 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
273 SAMR_USER_ACCESS_SET_PASSWORD
|
274 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
275 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
277 DEBUG(10, ("Creating account with flags: %d\n",acct_flags
));
279 result
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
288 if (!NT_STATUS_IS_OK(result
) &&
289 !NT_STATUS_EQUAL(result
, NT_STATUS_USER_EXISTS
)) {
290 d_fprintf(stderr
, "Creation of workstation account failed\n");
292 /* If NT_STATUS_ACCESS_DENIED then we have a valid
293 username/password combo but the user does not have
294 administrator access. */
296 if (NT_STATUS_V(result
) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED
))
297 d_fprintf(stderr
, "User specified does not have administrator privileges\n");
302 /* We *must* do this.... don't ask... */
304 if (NT_STATUS_IS_OK(result
)) {
305 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
308 CHECK_RPC_ERR_DEBUG(rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
314 ("error looking up rid for user %s: %s\n",
315 acct_name
, nt_errstr(result
)));
317 if (name_types
.ids
[0] != SID_NAME_USER
) {
318 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
, name_types
.ids
[0]));
322 user_rid
= user_rids
.ids
[0];
324 /* Open handle on user */
327 rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
329 SEC_RIGHTS_MAXIMUM_ALLOWED
,
332 ("could not re-open existing user %s: %s\n",
333 acct_name
, nt_errstr(result
)));
335 /* Create a random machine account password */
337 clear_trust_password
= generate_random_str(talloc_tos(), DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
338 E_md4hash(clear_trust_password
, md4_trust_password
);
340 /* Set password on machine account */
342 init_samr_CryptPassword(clear_trust_password
,
343 &cli
->user_session_key
,
346 set_info
.info24
.password
= crypt_pwd
;
347 set_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
349 CHECK_RPC_ERR(rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
353 "error setting trust account password");
355 /* Why do we have to try to (re-)set the ACB to be the same as what
356 we passed in the samr_create_dom_user() call? When a NT
357 workstation is joined to a domain by an administrator the
358 acb_info is set to 0x80. For a normal user with "Add
359 workstations to the domain" rights the acb_info is 0x84. I'm
360 not sure whether it is supposed to make a difference or not. NT
361 seems to cope with either value so don't bomb out if the set
362 userinfo2 level 0x10 fails. -tpot */
364 set_info
.info16
.acct_flags
= acb_info
;
366 /* Ignoring the return value is necessary for joining a domain
367 as a normal user with "Add workstation to domain" privilege. */
369 result
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
374 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
375 TALLOC_FREE(pipe_hnd
); /* Done with this pipe */
377 /* Now check the whole process from top-to-bottom */
379 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
381 if (!NT_STATUS_IS_OK(result
)) {
382 DEBUG(0,("Error connecting to NETLOGON pipe. Error was %s\n",
383 nt_errstr(result
) ));
387 result
= rpccli_netlogon_setup_creds(pipe_hnd
,
388 cli
->desthost
, /* server name */
390 global_myname(), /* client name */
391 global_myname(), /* machine account name */
396 if (!NT_STATUS_IS_OK(result
)) {
397 DEBUG(0, ("Error in domain join verification (credential setup failed): %s\n\n",
400 if ( NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
) &&
401 (sec_channel_type
== SEC_CHAN_BDC
) ) {
402 d_fprintf(stderr
, "Please make sure that no computer account\n"
403 "named like this machine (%s) exists in the domain\n",
410 /* We can only check the schannel connection if the client is allowed
411 to do this and the server supports it. If not, just assume success
412 (after all the rpccli_netlogon_setup_creds() succeeded, and we'll
413 do the same again (setup creds) in net_rpc_join_ok(). JRA. */
415 if (lp_client_schannel() && (neg_flags
& NETLOGON_NEG_SCHANNEL
)) {
416 struct rpc_pipe_client
*netlogon_schannel_pipe
;
418 result
= cli_rpc_pipe_open_schannel_with_key(
419 cli
, &ndr_table_netlogon
.syntax_id
,
420 PIPE_AUTH_LEVEL_PRIVACY
, domain
, pipe_hnd
->dc
,
421 &netlogon_schannel_pipe
);
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 TALLOC_FREE(netlogon_schannel_pipe
);
439 TALLOC_FREE(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(c
, 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 TALLOC_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(struct net_context
*c
, int argc
, const char **argv
)
487 if (c
->display_usage
) {
490 " Test if a join is OK\n");
494 /* Display success or failure */
495 nt_status
= net_rpc_join_ok(c
, c
->opt_target_workgroup
, NULL
, NULL
);
496 if (!NT_STATUS_IS_OK(nt_status
)) {
497 fprintf(stderr
,"Join to domain '%s' is not valid: %s\n",
498 c
->opt_target_workgroup
, nt_errstr(nt_status
));
502 printf("Join to '%s' is OK\n", c
->opt_target_workgroup
);