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"
24 /* Macro for checking RPC error codes to make things more readable */
26 #define CHECK_RPC_ERR(rpc, msg) \
27 if (!NT_STATUS_IS_OK(result = rpc)) { \
28 DEBUG(0, (msg ": %s\n", nt_errstr(result))); \
32 #define CHECK_RPC_ERR_DEBUG(rpc, debug_args) \
33 if (!NT_STATUS_IS_OK(result = rpc)) { \
34 DEBUG(0, debug_args); \
39 * confirm that a domain join is still valid
41 * @return A shell status integer (0 for success)
44 NTSTATUS
net_rpc_join_ok(const char *domain
, const char *server
,
45 struct sockaddr_storage
*pss
)
47 enum security_types sec
;
48 unsigned int conn_flags
= NET_FLAGS_PDC
;
49 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
50 struct cli_state
*cli
= NULL
;
51 struct rpc_pipe_client
*pipe_hnd
= NULL
;
52 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
53 NTSTATUS ntret
= NT_STATUS_UNSUCCESSFUL
;
55 sec
= (enum security_types
)lp_security();
58 /* Connect to IPC$ using machine account's credentials. We don't use anonymous
59 connection here, as it may be denied by server's local policy. */
60 net_use_machine_account();
63 /* some servers (e.g. WinNT) don't accept machine-authenticated
65 conn_flags
|= NET_FLAGS_ANONYMOUS
;
68 /* Connect to remote machine */
69 ntret
= net_make_ipc_connection_ex(domain
, server
, pss
, conn_flags
, &cli
);
70 if (!NT_STATUS_IS_OK(ntret
)) {
74 /* Setup the creds as though we're going to do schannel... */
75 netlogon_pipe
= get_schannel_session_key(cli
, domain
, &neg_flags
, &ntret
);
77 /* We return NT_STATUS_INVALID_NETWORK_RESPONSE if the server is refusing
78 to negotiate schannel, but the creds were set up ok. That'll have to do. */
81 if (NT_STATUS_EQUAL(ntret
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
85 DEBUG(0,("net_rpc_join_ok: failed to get schannel session "
86 "key from server %s for domain %s. Error was %s\n",
87 cli
->desthost
, domain
, nt_errstr(ntret
) ));
93 /* Only do the rest of the schannel test if the client is allowed to do this. */
94 if (!lp_client_schannel()) {
100 pipe_hnd
= cli_rpc_pipe_open_schannel_with_key(cli
, PI_NETLOGON
,
101 PIPE_AUTH_LEVEL_PRIVACY
,
102 domain
, netlogon_pipe
->dc
, &ntret
);
105 DEBUG(0,("net_rpc_join_ok: failed to open schannel session "
106 "on netlogon pipe to server %s for domain %s. Error was %s\n",
107 cli
->desthost
, domain
, nt_errstr(ntret
) ));
109 * Note: here, we have:
110 * (pipe_hnd != NULL) if and only if NT_STATUS_IS_OK(ntret)
119 * Join a domain using the administrator username and password
121 * @param argc Standard main() style argc
122 * @param argc Standard main() style argv. Initial components are already
123 * stripped. Currently not used.
124 * @return A shell status integer (0 for success)
128 int net_rpc_join_newstyle(int argc
, const char **argv
)
131 /* libsmb variables */
133 struct cli_state
*cli
;
135 uint32 acb_info
= ACB_WSTRUST
;
136 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
137 uint32 sec_channel_type
;
138 struct rpc_pipe_client
*pipe_hnd
= NULL
;
142 POLICY_HND lsa_pol
, sam_pol
, domain_pol
, user_pol
;
148 char *clear_trust_password
= NULL
;
149 struct samr_CryptPassword crypt_pwd
;
150 uchar md4_trust_password
[16];
151 union samr_UserInfo set_info
;
157 const char *domain
= NULL
;
159 struct lsa_String lsa_acct_name
;
161 uint32_t access_granted
= 0;
162 union lsa_PolicyInformation
*info
= NULL
;
163 struct samr_Ids user_rids
;
164 struct samr_Ids name_types
;
166 /* check what type of join */
168 sec_channel_type
= get_sec_channel_type(argv
[0]);
170 sec_channel_type
= get_sec_channel_type(NULL
);
173 switch (sec_channel_type
) {
175 acb_info
= ACB_WSTRUST
;
178 acb_info
= ACB_SVRTRUST
;
181 case SEC_CHAN_DOMAIN
:
182 acb_info
= ACB_DOMTRUST
;
187 /* Make authenticated connection to remote machine */
189 result
= net_make_ipc_connection(NET_FLAGS_PDC
, &cli
);
190 if (!NT_STATUS_IS_OK(result
)) {
194 if (!(mem_ctx
= talloc_init("net_rpc_join_newstyle"))) {
195 DEBUG(0, ("Could not initialise talloc context\n"));
199 /* Fetch domain sid */
201 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &result
);
203 DEBUG(0, ("Error connecting to LSA pipe. Error was %s\n",
204 nt_errstr(result
) ));
209 CHECK_RPC_ERR(rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, True
,
210 SEC_RIGHTS_MAXIMUM_ALLOWED
,
212 "error opening lsa policy handle");
214 CHECK_RPC_ERR(rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
216 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
218 "error querying info policy");
220 domain
= info
->account_domain
.name
.string
;
221 domain_sid
= info
->account_domain
.sid
;
223 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
224 cli_rpc_pipe_close(pipe_hnd
); /* Done with this pipe */
226 /* Bail out if domain didn't get set. */
228 DEBUG(0, ("Could not get domain name.\n"));
232 /* Create domain user */
233 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SAMR
, &result
);
235 DEBUG(0, ("Error connecting to SAM pipe. Error was %s\n",
236 nt_errstr(result
) ));
240 CHECK_RPC_ERR(rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
241 pipe_hnd
->cli
->desthost
,
242 SAMR_ACCESS_ENUM_DOMAINS
243 | SAMR_ACCESS_OPEN_DOMAIN
,
245 "could not connect to SAM database");
248 CHECK_RPC_ERR(rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
250 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
251 | SAMR_DOMAIN_ACCESS_CREATE_USER
252 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
255 "could not open domain");
257 /* Create domain user */
258 if ((acct_name
= talloc_asprintf(mem_ctx
, "%s$", global_myname())) == NULL
) {
259 result
= NT_STATUS_NO_MEMORY
;
262 strlower_m(acct_name
);
264 init_lsa_String(&lsa_acct_name
, acct_name
);
266 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
267 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
268 SAMR_USER_ACCESS_SET_PASSWORD
|
269 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
270 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
272 DEBUG(10, ("Creating account with flags: %d\n",acct_flags
));
274 result
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
283 if (!NT_STATUS_IS_OK(result
) &&
284 !NT_STATUS_EQUAL(result
, NT_STATUS_USER_EXISTS
)) {
285 d_fprintf(stderr
, "Creation of workstation account failed\n");
287 /* If NT_STATUS_ACCESS_DENIED then we have a valid
288 username/password combo but the user does not have
289 administrator access. */
291 if (NT_STATUS_V(result
) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED
))
292 d_fprintf(stderr
, "User specified does not have administrator privileges\n");
297 /* We *must* do this.... don't ask... */
299 if (NT_STATUS_IS_OK(result
)) {
300 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
303 CHECK_RPC_ERR_DEBUG(rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
309 ("error looking up rid for user %s: %s\n",
310 acct_name
, nt_errstr(result
)));
312 if (name_types
.ids
[0] != SID_NAME_USER
) {
313 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
, name_types
.ids
[0]));
317 user_rid
= user_rids
.ids
[0];
319 /* Open handle on user */
322 rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
324 SEC_RIGHTS_MAXIMUM_ALLOWED
,
327 ("could not re-open existing user %s: %s\n",
328 acct_name
, nt_errstr(result
)));
330 /* Create a random machine account password */
334 str
= generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
335 clear_trust_password
= SMB_STRDUP(str
);
336 E_md4hash(clear_trust_password
, md4_trust_password
);
339 /* Set password on machine account */
341 init_samr_CryptPassword(clear_trust_password
,
342 &cli
->user_session_key
,
345 init_samr_user_info24(&set_info
.info24
, &crypt_pwd
,
346 PASS_DONT_CHANGE_AT_NEXT_LOGON
);
348 CHECK_RPC_ERR(rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
352 "error setting trust account password");
354 /* Why do we have to try to (re-)set the ACB to be the same as what
355 we passed in the samr_create_dom_user() call? When a NT
356 workstation is joined to a domain by an administrator the
357 acb_info is set to 0x80. For a normal user with "Add
358 workstations to the domain" rights the acb_info is 0x84. I'm
359 not sure whether it is supposed to make a difference or not. NT
360 seems to cope with either value so don't bomb out if the set
361 userinfo2 level 0x10 fails. -tpot */
363 set_info
.info16
.acct_flags
= acb_info
;
365 /* Ignoring the return value is necessary for joining a domain
366 as a normal user with "Add workstation to domain" privilege. */
368 result
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
373 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
374 cli_rpc_pipe_close(pipe_hnd
); /* Done with this pipe */
376 /* Now check the whole process from top-to-bottom */
378 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_NETLOGON
, &result
);
380 DEBUG(0,("Error connecting to NETLOGON pipe. Error was %s\n",
381 nt_errstr(result
) ));
385 result
= rpccli_netlogon_setup_creds(pipe_hnd
,
386 cli
->desthost
, /* server name */
388 global_myname(), /* client name */
389 global_myname(), /* machine account name */
394 if (!NT_STATUS_IS_OK(result
)) {
395 DEBUG(0, ("Error in domain join verification (credential setup failed): %s\n\n",
398 if ( NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
) &&
399 (sec_channel_type
== SEC_CHAN_BDC
) ) {
400 d_fprintf(stderr
, "Please make sure that no computer account\n"
401 "named like this machine (%s) exists in the domain\n",
408 /* We can only check the schannel connection if the client is allowed
409 to do this and the server supports it. If not, just assume success
410 (after all the rpccli_netlogon_setup_creds() succeeded, and we'll
411 do the same again (setup creds) in net_rpc_join_ok(). JRA. */
413 if (lp_client_schannel() && (neg_flags
& NETLOGON_NEG_SCHANNEL
)) {
414 struct rpc_pipe_client
*netlogon_schannel_pipe
=
415 cli_rpc_pipe_open_schannel_with_key(cli
,
417 PIPE_AUTH_LEVEL_PRIVACY
,
422 if (!NT_STATUS_IS_OK(result
)) {
423 DEBUG(0, ("Error in domain join verification (schannel setup failed): %s\n\n",
426 if ( NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
) &&
427 (sec_channel_type
== SEC_CHAN_BDC
) ) {
428 d_fprintf(stderr
, "Please make sure that no computer account\n"
429 "named like this machine (%s) exists in the domain\n",
435 cli_rpc_pipe_close(netlogon_schannel_pipe
);
438 cli_rpc_pipe_close(pipe_hnd
);
440 /* Now store the secret in the secrets database */
442 strupper_m(CONST_DISCARD(char *, domain
));
444 if (!secrets_store_domain_sid(domain
, domain_sid
)) {
445 DEBUG(0, ("error storing domain sid for %s\n", domain
));
449 if (!secrets_store_machine_password(clear_trust_password
, domain
, sec_channel_type
)) {
450 DEBUG(0, ("error storing plaintext domain secrets for %s\n", domain
));
453 /* double-check, connection from scratch */
454 result
= net_rpc_join_ok(domain
, cli
->desthost
, &cli
->dest_ss
);
455 retval
= NT_STATUS_IS_OK(result
) ? 0 : -1;
459 /* Display success or failure */
463 fprintf(stderr
,"Unable to join domain %s.\n",domain
);
465 printf("Joined domain %s.\n",domain
);
471 SAFE_FREE(clear_trust_password
);
477 * check that a join is OK
479 * @return A shell status integer (0 for success)
482 int net_rpc_testjoin(int argc
, const char **argv
)
486 /* Display success or failure */
487 nt_status
= net_rpc_join_ok(opt_target_workgroup
, NULL
, NULL
);
488 if (!NT_STATUS_IS_OK(nt_status
)) {
489 fprintf(stderr
,"Join to domain '%s' is not valid: %s\n",
490 opt_target_workgroup
, nt_errstr(nt_status
));
494 printf("Join to '%s' is OK\n",opt_target_workgroup
);