Janitorial duties to make autogen.sh portable.
[Samba/gebeck_regimport.git] / source3 / utils / net_rpc_join.c
blobb0eb335986942e6fe2b3f5712e60baf1acca3c49
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
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include "includes.h"
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))); \
29 goto done; \
32 #define CHECK_RPC_ERR_DEBUG(rpc, debug_args) \
33 if (!NT_STATUS_IS_OK(result = rpc)) { \
34 DEBUG(0, debug_args); \
35 goto done; \
39 /**
40 * confirm that a domain join is still valid
42 * @return A shell status integer (0 for success)
44 **/
45 int net_rpc_join_ok(const char *domain)
47 struct cli_state *cli;
48 uchar stored_md4_trust_password[16];
49 int retval = 1;
50 uint32 channel;
51 NTSTATUS result;
52 uint32 neg_flags = 0x000001ff;
54 /* Connect to remote machine */
55 if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC))) {
56 return 1;
59 if (!cli_nt_session_open(cli, PI_NETLOGON)) {
60 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
61 goto done;
64 if (!secrets_fetch_trust_account_password(domain,
65 stored_md4_trust_password, NULL)) {
66 DEBUG(0,("Could not reterive domain trust secret"));
67 goto done;
70 if (lp_server_role() == ROLE_DOMAIN_BDC ||
71 lp_server_role() == ROLE_DOMAIN_PDC) {
72 channel = SEC_CHAN_BDC;
73 } else {
74 channel = SEC_CHAN_WKSTA;
77 CHECK_RPC_ERR(cli_nt_setup_creds(cli,
78 channel,
79 stored_md4_trust_password, &neg_flags, 2),
80 "error in domain join verification");
82 retval = 0; /* Success! */
84 done:
85 /* Close down pipe - this will clean up open policy handles */
86 if (cli->nt_pipe_fnum)
87 cli_nt_session_close(cli);
89 cli_shutdown(cli);
91 return retval;
94 /**
95 * Join a domain using the administrator username and password
97 * @param argc Standard main() style argc
98 * @param argc Standard main() style argv. Initial components are already
99 * stripped. Currently not used.
100 * @return A shell status integer (0 for success)
104 int net_rpc_join_newstyle(int argc, const char **argv)
107 /* libsmb variables */
109 struct cli_state *cli;
110 TALLOC_CTX *mem_ctx;
111 uint32 acb_info;
113 /* rpc variables */
115 POLICY_HND lsa_pol, sam_pol, domain_pol, user_pol;
116 DOM_SID domain_sid;
117 uint32 user_rid;
119 /* Password stuff */
121 char *clear_trust_password = NULL;
122 fstring ucs2_trust_password;
123 int ucs2_pw_len;
124 uchar pwbuf[516], sess_key[16];
125 SAM_USERINFO_CTR ctr;
126 SAM_USER_INFO_24 p24;
127 SAM_USER_INFO_10 p10;
129 /* Misc */
131 NTSTATUS result;
132 int retval = 1;
133 fstring domain;
134 uint32 num_rids, *name_types, *user_rids;
135 uint32 flags = 0x3e8;
136 char *acct_name;
137 const char *const_acct_name;
139 /* Connect to remote machine */
141 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC)))
142 return 1;
144 if (!(mem_ctx = talloc_init("net_rpc_join_newstyle"))) {
145 DEBUG(0, ("Could not initialise talloc context\n"));
146 goto done;
149 /* Fetch domain sid */
151 if (!cli_nt_session_open(cli, PI_LSARPC)) {
152 DEBUG(0, ("Error connecting to SAM pipe\n"));
153 goto done;
157 CHECK_RPC_ERR(cli_lsa_open_policy(cli, mem_ctx, True,
158 SEC_RIGHTS_MAXIMUM_ALLOWED,
159 &lsa_pol),
160 "error opening lsa policy handle");
162 CHECK_RPC_ERR(cli_lsa_query_info_policy(cli, mem_ctx, &lsa_pol,
163 5, domain, &domain_sid),
164 "error querying info policy");
166 cli_lsa_close(cli, mem_ctx, &lsa_pol);
168 cli_nt_session_close(cli); /* Done with this pipe */
170 /* Create domain user */
171 if (!cli_nt_session_open(cli, PI_SAMR)) {
172 DEBUG(0, ("Error connecting to SAM pipe\n"));
173 goto done;
176 CHECK_RPC_ERR(cli_samr_connect(cli, mem_ctx,
177 SEC_RIGHTS_MAXIMUM_ALLOWED,
178 &sam_pol),
179 "could not connect to SAM database");
182 CHECK_RPC_ERR(cli_samr_open_domain(cli, mem_ctx, &sam_pol,
183 SEC_RIGHTS_MAXIMUM_ALLOWED,
184 &domain_sid, &domain_pol),
185 "could not open domain");
187 /* Create domain user */
188 acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname());
189 strlower(acct_name);
190 const_acct_name = acct_name;
192 acb_info = ((lp_server_role() == ROLE_DOMAIN_BDC) || lp_server_role() == ROLE_DOMAIN_PDC) ? ACB_SVRTRUST : ACB_WSTRUST;
194 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
195 acct_name, acb_info,
196 0xe005000b, &user_pol,
197 &user_rid);
199 if (!NT_STATUS_IS_OK(result) &&
200 !NT_STATUS_EQUAL(result, NT_STATUS_USER_EXISTS)) {
201 d_printf("Create of workstation account failed\n");
203 /* If NT_STATUS_ACCESS_DENIED then we have a valid
204 username/password combo but the user does not have
205 administrator access. */
207 if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED))
208 d_printf("User specified does not have administrator privileges\n");
210 goto done;
213 /* We *must* do this.... don't ask... */
215 if (NT_STATUS_IS_OK(result))
216 cli_samr_close(cli, mem_ctx, &user_pol);
218 CHECK_RPC_ERR_DEBUG(cli_samr_lookup_names(cli, mem_ctx,
219 &domain_pol, flags,
220 1, &const_acct_name,
221 &num_rids,
222 &user_rids, &name_types),
223 ("error looking up rid for user %s: %s\n",
224 acct_name, nt_errstr(result)));
226 if (name_types[0] != SID_NAME_USER) {
227 DEBUG(0, ("%s is not a user account\n", acct_name));
228 goto done;
231 user_rid = user_rids[0];
233 /* Open handle on user */
235 CHECK_RPC_ERR_DEBUG(
236 cli_samr_open_user(cli, mem_ctx, &domain_pol,
237 SEC_RIGHTS_MAXIMUM_ALLOWED,
238 user_rid, &user_pol),
239 ("could not re-open existing user %s: %s\n",
240 acct_name, nt_errstr(result)));
242 /* Create a random machine account password */
245 char *str;
246 str = generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
247 clear_trust_password = strdup(str);
250 ucs2_pw_len = push_ucs2(NULL, ucs2_trust_password,
251 clear_trust_password,
252 sizeof(ucs2_trust_password), 0);
254 encode_pw_buffer((char *)pwbuf, ucs2_trust_password,
255 ucs2_pw_len);
257 /* Set password on machine account */
259 ZERO_STRUCT(ctr);
260 ZERO_STRUCT(p24);
262 init_sam_user_info24(&p24, (char *)pwbuf,24);
264 ctr.switch_value = 24;
265 ctr.info.id24 = &p24;
267 CHECK_RPC_ERR(cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
268 cli->user_session_key, &ctr),
269 "error setting trust account password");
271 /* Why do we have to try to (re-)set the ACB to be the same as what
272 we passed in the samr_create_dom_user() call? When a NT
273 workstation is joined to a domain by an administrator the
274 acb_info is set to 0x80. For a normal user with "Add
275 workstations to the domain" rights the acb_info is 0x84. I'm
276 not sure whether it is supposed to make a difference or not. NT
277 seems to cope with either value so don't bomb out if the set
278 userinfo2 level 0x10 fails. -tpot */
280 ZERO_STRUCT(ctr);
281 ctr.switch_value = 0x10;
282 ctr.info.id10 = &p10;
284 init_sam_user_info10(&p10, acb_info);
286 /* Ignoring the return value is necessary for joining a domain
287 as a normal user with "Add workstation to domain" privilege. */
289 result = cli_samr_set_userinfo2(cli, mem_ctx, &user_pol, 0x10,
290 sess_key, &ctr);
292 /* Now store the secret in the secrets database */
294 strupper(domain);
296 if (!secrets_store_domain_sid(domain, &domain_sid)) {
297 DEBUG(0, ("error storing domain sid for %s\n", domain));
298 goto done;
301 if (!secrets_store_machine_password(clear_trust_password)) {
302 DEBUG(0, ("error storing plaintext domain secrets for %s\n", domain));
305 /* Now check the whole process from top-to-bottom */
306 cli_samr_close(cli, mem_ctx, &user_pol);
307 cli_nt_session_close(cli); /* Done with this pipe */
309 retval = net_rpc_join_ok(domain);
311 done:
312 /* Close down pipe - this will clean up open policy handles */
314 if (cli->nt_pipe_fnum)
315 cli_nt_session_close(cli);
317 /* Display success or failure */
319 if (retval != 0) {
320 trust_password_delete(domain);
321 fprintf(stderr,"Unable to join domain %s.\n",domain);
322 } else {
323 printf("Joined domain %s.\n",domain);
326 cli_shutdown(cli);
328 SAFE_FREE(clear_trust_password);
330 return retval;
335 * check that a join is OK
337 * @return A shell status integer (0 for success)
340 int net_rpc_testjoin(int argc, const char **argv)
342 char *domain = smb_xstrdup(lp_workgroup());
344 /* Display success or failure */
345 if (net_rpc_join_ok(domain) != 0) {
346 fprintf(stderr,"Join to domain '%s' is not valid\n",domain);
347 free(domain);
348 return -1;
351 printf("Join to '%s' is OK\n",domain);
352 free(domain);
353 return 0;