Consolidate user create/delete paths in smbpasswd
[Samba.git] / source3 / utils / net_rpc_join.c
blobcae2491aed4d6414d016a052757249b6118949c2
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"
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))); \
30 goto done; \
33 #define CHECK_RPC_ERR_DEBUG(rpc, debug_args) \
34 if (!NT_STATUS_IS_OK(result = rpc)) { \
35 DEBUG(0, debug_args); \
36 goto done; \
39 /**
40 * confirm that a domain join is still valid
42 * @return A shell status integer (0 for success)
44 **/
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();
58 if (sec == SEC_ADS) {
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 set_cmdline_auth_info_use_machine_account(c->auth_info);
62 set_cmdline_auth_info_machine_account_creds(c->auth_info);
64 } else {
65 /* some servers (e.g. WinNT) don't accept machine-authenticated
66 smb connections */
67 conn_flags |= NET_FLAGS_ANONYMOUS;
70 /* Connect to remote machine */
71 ntret = net_make_ipc_connection_ex(c, domain, server, pss, conn_flags,
72 &cli);
73 if (!NT_STATUS_IS_OK(ntret)) {
74 return ntret;
77 /* Setup the creds as though we're going to do schannel... */
78 ntret = get_schannel_session_key(cli, domain, &neg_flags,
79 &netlogon_pipe);
81 /* We return NT_STATUS_INVALID_NETWORK_RESPONSE if the server is refusing
82 to negotiate schannel, but the creds were set up ok. That'll have to do. */
84 if (!NT_STATUS_IS_OK(ntret)) {
85 if (NT_STATUS_EQUAL(ntret, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
86 cli_shutdown(cli);
87 return NT_STATUS_OK;
88 } else {
89 DEBUG(0,("net_rpc_join_ok: failed to get schannel session "
90 "key from server %s for domain %s. Error was %s\n",
91 cli->desthost, domain, nt_errstr(ntret) ));
92 cli_shutdown(cli);
93 return ntret;
97 /* Only do the rest of the schannel test if the client is allowed to do this. */
98 if (!lp_client_schannel()) {
99 cli_shutdown(cli);
100 /* We're good... */
101 return ntret;
104 ntret = cli_rpc_pipe_open_schannel_with_key(
105 cli, &ndr_table_netlogon.syntax_id, PIPE_AUTH_LEVEL_PRIVACY,
106 domain, &netlogon_pipe->dc, &pipe_hnd);
108 if (!NT_STATUS_IS_OK(ntret)) {
109 DEBUG(0,("net_rpc_join_ok: failed to open schannel session "
110 "on netlogon pipe to server %s for domain %s. Error was %s\n",
111 cli->desthost, domain, nt_errstr(ntret) ));
113 * Note: here, we have:
114 * (pipe_hnd != NULL) if and only if NT_STATUS_IS_OK(ntret)
118 cli_shutdown(cli);
119 return ntret;
123 * Join a domain using the administrator username and password
125 * @param argc Standard main() style argc
126 * @param argc Standard main() style argv. Initial components are already
127 * stripped. Currently not used.
128 * @return A shell status integer (0 for success)
132 int net_rpc_join_newstyle(struct net_context *c, int argc, const char **argv)
135 /* libsmb variables */
137 struct cli_state *cli;
138 TALLOC_CTX *mem_ctx;
139 uint32 acb_info = ACB_WSTRUST;
140 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
141 uint32 sec_channel_type;
142 struct rpc_pipe_client *pipe_hnd = NULL;
144 /* rpc variables */
146 struct policy_handle lsa_pol, sam_pol, domain_pol, user_pol;
147 DOM_SID *domain_sid;
148 uint32 user_rid;
150 /* Password stuff */
152 char *clear_trust_password = NULL;
153 struct samr_CryptPassword crypt_pwd;
154 uchar md4_trust_password[16];
155 union samr_UserInfo set_info;
157 /* Misc */
159 NTSTATUS result;
160 int retval = 1;
161 const char *domain = NULL;
162 char *acct_name;
163 struct lsa_String lsa_acct_name;
164 uint32 acct_flags=0;
165 uint32_t access_granted = 0;
166 union lsa_PolicyInformation *info = NULL;
167 struct samr_Ids user_rids;
168 struct samr_Ids name_types;
170 /* check what type of join */
171 if (argc >= 0) {
172 sec_channel_type = get_sec_channel_type(argv[0]);
173 } else {
174 sec_channel_type = get_sec_channel_type(NULL);
177 switch (sec_channel_type) {
178 case SEC_CHAN_WKSTA:
179 acb_info = ACB_WSTRUST;
180 break;
181 case SEC_CHAN_BDC:
182 acb_info = ACB_SVRTRUST;
183 break;
184 #if 0
185 case SEC_CHAN_DOMAIN:
186 acb_info = ACB_DOMTRUST;
187 break;
188 #endif
191 /* Make authenticated connection to remote machine */
193 result = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
194 if (!NT_STATUS_IS_OK(result)) {
195 return 1;
198 if (!(mem_ctx = talloc_init("net_rpc_join_newstyle"))) {
199 DEBUG(0, ("Could not initialise talloc context\n"));
200 goto done;
203 /* Fetch domain sid */
205 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
206 &pipe_hnd);
207 if (!NT_STATUS_IS_OK(result)) {
208 DEBUG(0, ("Error connecting to LSA pipe. Error was %s\n",
209 nt_errstr(result) ));
210 goto done;
214 CHECK_RPC_ERR(rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
215 SEC_FLAG_MAXIMUM_ALLOWED,
216 &lsa_pol),
217 "error opening lsa policy handle");
219 CHECK_RPC_ERR(rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
220 &lsa_pol,
221 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
222 &info),
223 "error querying info policy");
225 domain = info->account_domain.name.string;
226 domain_sid = info->account_domain.sid;
228 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
229 TALLOC_FREE(pipe_hnd); /* Done with this pipe */
231 /* Bail out if domain didn't get set. */
232 if (!domain) {
233 DEBUG(0, ("Could not get domain name.\n"));
234 goto done;
237 /* Create domain user */
238 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
239 &pipe_hnd);
240 if (!NT_STATUS_IS_OK(result)) {
241 DEBUG(0, ("Error connecting to SAM pipe. Error was %s\n",
242 nt_errstr(result) ));
243 goto done;
246 CHECK_RPC_ERR(rpccli_samr_Connect2(pipe_hnd, mem_ctx,
247 pipe_hnd->desthost,
248 SAMR_ACCESS_ENUM_DOMAINS
249 | SAMR_ACCESS_LOOKUP_DOMAIN,
250 &sam_pol),
251 "could not connect to SAM database");
254 CHECK_RPC_ERR(rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
255 &sam_pol,
256 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
257 | SAMR_DOMAIN_ACCESS_CREATE_USER
258 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
259 domain_sid,
260 &domain_pol),
261 "could not open domain");
263 /* Create domain user */
264 if ((acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname())) == NULL) {
265 result = NT_STATUS_NO_MEMORY;
266 goto done;
268 strlower_m(acct_name);
270 init_lsa_String(&lsa_acct_name, acct_name);
272 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
273 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
274 SAMR_USER_ACCESS_SET_PASSWORD |
275 SAMR_USER_ACCESS_GET_ATTRIBUTES |
276 SAMR_USER_ACCESS_SET_ATTRIBUTES;
278 DEBUG(10, ("Creating account with flags: %d\n",acct_flags));
280 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
281 &domain_pol,
282 &lsa_acct_name,
283 acb_info,
284 acct_flags,
285 &user_pol,
286 &access_granted,
287 &user_rid);
289 if (!NT_STATUS_IS_OK(result) &&
290 !NT_STATUS_EQUAL(result, NT_STATUS_USER_EXISTS)) {
291 d_fprintf(stderr, "Creation of workstation account failed\n");
293 /* If NT_STATUS_ACCESS_DENIED then we have a valid
294 username/password combo but the user does not have
295 administrator access. */
297 if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED))
298 d_fprintf(stderr, "User specified does not have administrator privileges\n");
300 goto done;
303 /* We *must* do this.... don't ask... */
305 if (NT_STATUS_IS_OK(result)) {
306 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
309 CHECK_RPC_ERR_DEBUG(rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
310 &domain_pol,
312 &lsa_acct_name,
313 &user_rids,
314 &name_types),
315 ("error looking up rid for user %s: %s\n",
316 acct_name, nt_errstr(result)));
318 if (name_types.ids[0] != SID_NAME_USER) {
319 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name, name_types.ids[0]));
320 goto done;
323 user_rid = user_rids.ids[0];
325 /* Open handle on user */
327 CHECK_RPC_ERR_DEBUG(
328 rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
329 &domain_pol,
330 SEC_FLAG_MAXIMUM_ALLOWED,
331 user_rid,
332 &user_pol),
333 ("could not re-open existing user %s: %s\n",
334 acct_name, nt_errstr(result)));
336 /* Create a random machine account password */
338 clear_trust_password = generate_random_str(talloc_tos(), DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
339 E_md4hash(clear_trust_password, md4_trust_password);
341 /* Set password on machine account */
343 init_samr_CryptPassword(clear_trust_password,
344 &cli->user_session_key,
345 &crypt_pwd);
347 set_info.info24.password = crypt_pwd;
348 set_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
350 CHECK_RPC_ERR(rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
351 &user_pol,
353 &set_info),
354 "error setting trust account password");
356 /* Why do we have to try to (re-)set the ACB to be the same as what
357 we passed in the samr_create_dom_user() call? When a NT
358 workstation is joined to a domain by an administrator the
359 acb_info is set to 0x80. For a normal user with "Add
360 workstations to the domain" rights the acb_info is 0x84. I'm
361 not sure whether it is supposed to make a difference or not. NT
362 seems to cope with either value so don't bomb out if the set
363 userinfo2 level 0x10 fails. -tpot */
365 set_info.info16.acct_flags = acb_info;
367 /* Ignoring the return value is necessary for joining a domain
368 as a normal user with "Add workstation to domain" privilege. */
370 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
371 &user_pol,
373 &set_info);
375 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
376 TALLOC_FREE(pipe_hnd); /* Done with this pipe */
378 /* Now check the whole process from top-to-bottom */
380 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
381 &pipe_hnd);
382 if (!NT_STATUS_IS_OK(result)) {
383 DEBUG(0,("Error connecting to NETLOGON pipe. Error was %s\n",
384 nt_errstr(result) ));
385 goto done;
388 result = rpccli_netlogon_setup_creds(pipe_hnd,
389 cli->desthost, /* server name */
390 domain, /* domain */
391 global_myname(), /* client name */
392 global_myname(), /* machine account name */
393 md4_trust_password,
394 sec_channel_type,
395 &neg_flags);
397 if (!NT_STATUS_IS_OK(result)) {
398 DEBUG(0, ("Error in domain join verification (credential setup failed): %s\n\n",
399 nt_errstr(result)));
401 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) &&
402 (sec_channel_type == SEC_CHAN_BDC) ) {
403 d_fprintf(stderr, "Please make sure that no computer account\n"
404 "named like this machine (%s) exists in the domain\n",
405 global_myname());
408 goto done;
411 /* We can only check the schannel connection if the client is allowed
412 to do this and the server supports it. If not, just assume success
413 (after all the rpccli_netlogon_setup_creds() succeeded, and we'll
414 do the same again (setup creds) in net_rpc_join_ok(). JRA. */
416 if (lp_client_schannel() && (neg_flags & NETLOGON_NEG_SCHANNEL)) {
417 struct rpc_pipe_client *netlogon_schannel_pipe;
419 result = cli_rpc_pipe_open_schannel_with_key(
420 cli, &ndr_table_netlogon.syntax_id,
421 PIPE_AUTH_LEVEL_PRIVACY, domain, &pipe_hnd->dc,
422 &netlogon_schannel_pipe);
424 if (!NT_STATUS_IS_OK(result)) {
425 DEBUG(0, ("Error in domain join verification (schannel setup failed): %s\n\n",
426 nt_errstr(result)));
428 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) &&
429 (sec_channel_type == SEC_CHAN_BDC) ) {
430 d_fprintf(stderr, "Please make sure that no computer account\n"
431 "named like this machine (%s) exists in the domain\n",
432 global_myname());
435 goto done;
437 TALLOC_FREE(netlogon_schannel_pipe);
440 TALLOC_FREE(pipe_hnd);
442 /* Now store the secret in the secrets database */
444 strupper_m(CONST_DISCARD(char *, domain));
446 if (!secrets_store_domain_sid(domain, domain_sid)) {
447 DEBUG(0, ("error storing domain sid for %s\n", domain));
448 goto done;
451 if (!secrets_store_machine_password(clear_trust_password, domain, sec_channel_type)) {
452 DEBUG(0, ("error storing plaintext domain secrets for %s\n", domain));
455 /* double-check, connection from scratch */
456 result = net_rpc_join_ok(c, domain, cli->desthost, &cli->dest_ss);
457 retval = NT_STATUS_IS_OK(result) ? 0 : -1;
459 done:
461 /* Display success or failure */
463 if (domain) {
464 if (retval != 0) {
465 fprintf(stderr,"Unable to join domain %s.\n",domain);
466 } else {
467 printf("Joined domain %s.\n",domain);
471 cli_shutdown(cli);
473 TALLOC_FREE(clear_trust_password);
475 return retval;
479 * check that a join is OK
481 * @return A shell status integer (0 for success)
484 int net_rpc_testjoin(struct net_context *c, int argc, const char **argv)
486 NTSTATUS nt_status;
488 if (c->display_usage) {
489 d_printf("Usage\n"
490 "net rpc testjoin\n"
491 " Test if a join is OK\n");
492 return 0;
495 /* Display success or failure */
496 nt_status = net_rpc_join_ok(c, c->opt_target_workgroup, NULL, NULL);
497 if (!NT_STATUS_IS_OK(nt_status)) {
498 fprintf(stderr,"Join to domain '%s' is not valid: %s\n",
499 c->opt_target_workgroup, nt_errstr(nt_status));
500 return -1;
503 printf("Join to '%s' is OK\n", c->opt_target_workgroup);
504 return 0;