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. */
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 * Join a domain using the administrator username and password
41 * @param argc Standard main() style argc
42 * @param argc Standard main() style argv. Initial components are already
43 * stripped. Currently not used.
44 * @return A shell status integer (0 for success)
48 int net_rpc_join_newstyle(int argc, const char **argv)
51 extern pstring global_myname;
53 /* libsmb variables */
55 struct cli_state *cli;
61 POLICY_HND lsa_pol, sam_pol, domain_pol, user_pol;
67 char *clear_trust_password = NULL;
68 fstring ucs2_trust_password;
70 uchar stored_md4_trust_password[16];
71 uchar pwbuf[516], sess_key[16];
81 uint32 num_rids, *name_types, *user_rids;
83 const char *acct_name;
85 /* Connect to remote machine */
87 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC)))
90 if (!(mem_ctx = talloc_init())) {
91 DEBUG(0, ("Could not initialise talloc context\n"));
95 /* Fetch domain sid */
97 if (!cli_nt_session_open(cli, PIPE_LSARPC)) {
98 DEBUG(0, ("Error connecting to SAM pipe\n"));
103 CHECK_RPC_ERR(cli_lsa_open_policy(cli, mem_ctx, True,
104 SEC_RIGHTS_MAXIMUM_ALLOWED,
106 "error opening lsa policy handle");
108 CHECK_RPC_ERR(cli_lsa_query_info_policy(cli, mem_ctx, &lsa_pol,
109 5, domain, &domain_sid),
110 "error querying info policy");
112 cli_lsa_close(cli, mem_ctx, &lsa_pol);
114 cli_nt_session_close(cli); /* Done with this pipe */
116 /* Create domain user */
117 if (!cli_nt_session_open(cli, PIPE_SAMR)) {
118 DEBUG(0, ("Error connecting to SAM pipe\n"));
122 CHECK_RPC_ERR(cli_samr_connect(cli, mem_ctx,
123 SEC_RIGHTS_MAXIMUM_ALLOWED,
125 "could not connect to SAM database");
128 CHECK_RPC_ERR(cli_samr_open_domain(cli, mem_ctx, &sam_pol,
129 SEC_RIGHTS_MAXIMUM_ALLOWED,
130 &domain_sid, &domain_pol),
131 "could not open domain");
133 /* Create domain user */
134 acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname);
137 acb_info = ((lp_server_role() == ROLE_DOMAIN_BDC) || lp_server_role() == ROLE_DOMAIN_PDC) ? ACB_SVRTRUST : ACB_WSTRUST;
139 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
141 0xe005000b, &user_pol,
144 if (!NT_STATUS_IS_OK(result) &&
145 !NT_STATUS_EQUAL(result, NT_STATUS_USER_EXISTS)) {
146 d_printf("Create of workstation account failed\n");
148 /* If NT_STATUS_ACCESS_DENIED then we have a valid
149 username/password combo but the user does not have
150 administrator access. */
152 if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED))
153 d_printf("User specified does not have administrator privileges\n");
158 /* We *must* do this.... don't ask... */
160 if (NT_STATUS_IS_OK(result))
161 cli_samr_close(cli, mem_ctx, &user_pol);
163 CHECK_RPC_ERR_DEBUG(cli_samr_lookup_names(cli, mem_ctx,
165 1, &acct_name, &num_rids,
166 &user_rids, &name_types),
167 ("error looking up rid for user %s: %s\n",
168 acct_name, nt_errstr(result)));
170 if (name_types[0] != SID_NAME_USER) {
171 DEBUG(0, ("%s is not a user account\n", acct_name));
175 user_rid = user_rids[0];
177 /* Open handle on user */
180 cli_samr_open_user(cli, mem_ctx, &domain_pol,
181 SEC_RIGHTS_MAXIMUM_ALLOWED,
182 user_rid, &user_pol),
183 ("could not re-open existing user %s: %s\n",
184 acct_name, nt_errstr(result)));
186 /* Create a random machine account password */
190 str = generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
191 clear_trust_password = strdup(str);
194 ucs2_pw_len = push_ucs2(NULL, ucs2_trust_password,
195 clear_trust_password,
196 sizeof(ucs2_trust_password), 0);
198 encode_pw_buffer((char *)pwbuf, ucs2_trust_password,
201 /* Set password on machine account */
206 init_sam_user_info24(&p24, (char *)pwbuf,24);
208 ctr.switch_value = 24;
209 ctr.info.id24 = &p24;
211 /* I don't think this is quite the right place for this
212 calculation. It should be moved somewhere where the credentials
213 are calculated. )-: */
215 mdfour(sess_key, cli->pwd.smb_nt_pwd, 16);
217 CHECK_RPC_ERR(cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
219 "error setting trust account password");
221 /* Why do we have to try to (re-)set the ACB to be the same as what
222 we passed in the samr_create_dom_user() call? When a NT
223 workstation is joined to a domain by an administrator the
224 acb_info is set to 0x80. For a normal user with "Add
225 workstations to the domain" rights the acb_info is 0x84. I'm
226 not sure whether it is supposed to make a difference or not. NT
227 seems to cope with either value so don't bomb out if the set
228 userinfo2 level 0x10 fails. -tpot */
231 ctr.switch_value = 0x10;
232 ctr.info.id10 = &p10;
234 init_sam_user_info10(&p10, acb_info);
236 /* Ignoring the return value is necessary for joining a domain
237 as a normal user with "Add workstation to domain" privilege. */
239 result = cli_samr_set_userinfo2(cli, mem_ctx, &user_pol, 0x10,
242 /* Now store the secret in the secrets database */
246 if (!secrets_store_domain_sid(domain, &domain_sid)) {
247 DEBUG(0, ("error storing domain sid for %s\n", domain));
251 if (!secrets_store_machine_password(clear_trust_password)) {
252 DEBUG(0, ("error storing plaintext domain secrets for %s\n", domain));
255 /* Now check the whole process from top-to-bottom */
257 cli_samr_close(cli, mem_ctx, &user_pol);
259 cli_nt_session_close(cli); /* Done with this pipe */
261 if (!cli_nt_session_open(cli, PIPE_NETLOGON)) {
262 DEBUG(0, ("Error connecting to NETLOGON pipe\n"));
266 if (!secrets_fetch_trust_account_password(domain,
267 stored_md4_trust_password, NULL)) {
268 DEBUG(0, ("Could not reterive secrets we just stored!"));
272 CHECK_RPC_ERR(new_cli_nt_setup_creds(cli,
273 (acb_info & ACB_SVRTRUST) ? SEC_CHAN_BDC : SEC_CHAN_WKSTA,
274 stored_md4_trust_password),
275 "error in domain join verification");
277 retval = 0; /* Success! */
280 /* Close down pipe - this will clean up open policy handles */
282 if (cli->nt_pipe_fnum)
283 cli_nt_session_close(cli);
285 /* Display success or failure */
288 trust_password_delete(domain);
289 fprintf(stderr,"Unable to join domain %s.\n",domain);
291 printf("Joined domain %s.\n",domain);
296 SAFE_FREE(clear_trust_password);