Merge from 2.2 to allow net rpc join -U to complete even if the workstation
[samba.git] / source3 / utils / net_rpc_join.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    Version 3.0
4    Distributed SMB/CIFS Server Management Utility 
5    Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
6    Copyright (C) Tim Potter     2001
7
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 2 of the License, or
11    (at your option) any later version.
12    
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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
21  
22 #include "includes.h"
23 #include "../utils/net.h"
24
25 /* Macro for checking RPC error codes to make things more readable */
26
27 #define CHECK_RPC_ERR(rpc, msg) \
28         if (!NT_STATUS_IS_OK(result = rpc)) { \
29                 DEBUG(0, (msg ": %s\n", get_nt_error_msg(result))); \
30                 goto done; \
31         }
32
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; \
37         }
38
39 /*********************************************************
40 Join a domain using the administrator username and password
41 **********************************************************/
42
43 int net_rpc_join(int argc, const char **argv) 
44 {
45
46         extern pstring global_myname;
47
48         /* libsmb variables */
49
50         struct cli_state *cli;
51         fstring acct_name;
52         TALLOC_CTX *mem_ctx;
53         uint32 acb_info;
54
55         /* rpc variables */
56
57         POLICY_HND lsa_pol, sam_pol, domain_pol, user_pol;
58         DOM_SID domain_sid;
59         uint32 user_rid;
60
61         /* Password stuff */
62
63         char *clear_trust_password = NULL;
64         fstring ucs2_trust_password;
65         int ucs2_pw_len;
66         uchar stored_md4_trust_password[16];
67         uchar pwbuf[516], sess_key[16];
68         SAM_USERINFO_CTR ctr;
69         SAM_USER_INFO_24 p24;
70         SAM_USER_INFO_10 p10;
71
72         /* Misc */
73
74         NTSTATUS result;
75         int retval = 1;
76         fstring domain;
77         uint32 num_rids, *name_types, *user_rids;
78         uint32 flags = 0x3e8;
79         char *names;
80         
81         /* Connect to remote machine */
82
83         if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) 
84                 return 1;
85
86         if (!(mem_ctx = talloc_init())) {
87                 DEBUG(0, ("Could not initialise talloc context\n"));
88                 goto done;
89         }
90
91         /* Fetch domain sid */
92
93         if (!cli_nt_session_open(cli, PIPE_LSARPC)) {
94                 DEBUG(0, ("Error connecting to SAM pipe\n"));
95                 goto done;
96         }
97
98
99         CHECK_RPC_ERR(cli_lsa_open_policy(cli, mem_ctx, True,
100                                           SEC_RIGHTS_MAXIMUM_ALLOWED,
101                                           &lsa_pol),
102                       "error opening lsa policy handle");
103
104         CHECK_RPC_ERR(cli_lsa_query_info_policy(cli, mem_ctx, &lsa_pol,
105                                                 5, domain, &domain_sid),
106                       "error querying info policy");
107
108         cli_lsa_close(cli, mem_ctx, &lsa_pol);
109
110         cli_nt_session_close(cli); /* Done with this pipe */
111
112         /* Create domain user */
113         if (!cli_nt_session_open(cli, PIPE_SAMR)) {
114                 DEBUG(0, ("Error connecting to SAM pipe\n"));
115                 goto done;
116         }
117
118         CHECK_RPC_ERR(cli_samr_connect(cli, mem_ctx, 
119                                        SEC_RIGHTS_MAXIMUM_ALLOWED,
120                                        &sam_pol),
121                       "could not connect to SAM database");
122
123         
124         CHECK_RPC_ERR(cli_samr_open_domain(cli, mem_ctx, &sam_pol,
125                                            SEC_RIGHTS_MAXIMUM_ALLOWED,
126                                            &domain_sid, &domain_pol),
127                       "could not open domain");
128
129         /* Create domain user */
130         fstrcpy(acct_name, global_myname);
131         fstrcat(acct_name, "$");
132         strlower(acct_name);
133
134         acb_info = (lp_server_role() == ROLE_DOMAIN_BDC) ? ACB_SVRTRUST : ACB_WSTRUST;
135
136         result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
137                                           acct_name, acb_info,
138                                           0xe005000b, &user_pol, 
139                                           &user_rid);
140
141         if (!NT_STATUS_IS_OK(result) && 
142             !NT_STATUS_EQUAL(result, NT_STATUS_USER_EXISTS)) {
143                 d_printf("Create of workstation account failed\n");
144                 goto done;
145         }
146
147         /* We *must* do this.... don't ask... */
148
149         if (NT_STATUS_IS_OK(result))
150                 cli_samr_close(cli, mem_ctx, &user_pol);
151
152         names = (char *)&acct_name[0];
153
154         CHECK_RPC_ERR_DEBUG(cli_samr_lookup_names(cli, mem_ctx,
155                                                   &domain_pol, flags,
156                                                   1, &names, &num_rids,
157                                                   &user_rids, &name_types),
158                             ("error looking up rid for user %s: %s\n",
159                              acct_name, get_nt_error_msg(result)));
160
161         if (name_types[0] != SID_NAME_USER) {
162                 DEBUG(0, ("%s is not a user account\n", acct_name));
163                 goto done;
164         }
165
166         user_rid = user_rids[0];
167                 
168         /* Open handle on user */
169
170         CHECK_RPC_ERR_DEBUG(
171                 cli_samr_open_user(cli, mem_ctx, &domain_pol,
172                                    SEC_RIGHTS_MAXIMUM_ALLOWED,
173                                    user_rid, &user_pol),
174                 ("could not re-open existing user %s: %s\n",
175                  acct_name, get_nt_error_msg(result)));
176         
177         /* Create a random machine account password */
178
179         { 
180                 char *str;
181                 str = generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
182                 clear_trust_password = strdup(str);
183         }
184
185         ucs2_pw_len = push_ucs2(NULL, ucs2_trust_password, 
186                                 clear_trust_password, 
187                                 sizeof(ucs2_trust_password), 0);
188                   
189         encode_pw_buffer((char *)pwbuf, ucs2_trust_password,
190                          ucs2_pw_len);
191
192         /* Set password on machine account */
193
194         ZERO_STRUCT(ctr);
195         ZERO_STRUCT(p24);
196
197         init_sam_user_info24(&p24, (char *)pwbuf,24);
198
199         ctr.switch_value = 24;
200         ctr.info.id24 = &p24;
201
202         /* I don't think this is quite the right place for this
203            calculation.  It should be moved somewhere where the credentials
204            are calculated. )-: */
205
206         mdfour(sess_key, cli->pwd.smb_nt_pwd, 16);
207
208         CHECK_RPC_ERR(cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24, 
209                                             sess_key, &ctr),
210                       "error setting trust account password");
211
212         /* Why do we have to try to (re-)set the ACB to be the same as what
213            we passed in the samr_create_dom_user() call?  When a NT
214            workstation is joined to a domain by an administrator the
215            acb_info is set to 0x80.  For a normal user with "Add
216            workstations to the domain" rights the acb_info is 0x84.  I'm
217            not sure whether it is supposed to make a difference or not.  NT
218            seems to cope with either value so don't bomb out if the set
219            userinfo2 level 0x10 fails.  -tpot */
220
221         ZERO_STRUCT(ctr);
222         ctr.switch_value = 0x10;
223         ctr.info.id10 = &p10;
224
225         init_sam_user_info10(&p10, acb_info);
226
227         /* Ignoring the return value is necessary for joining a domain
228            as a normal user with "Add workstation to domain" privilege. */
229
230         result = cli_samr_set_userinfo2(cli, mem_ctx, &user_pol, 0x10, 
231                                         sess_key, &ctr);
232
233         /* Now store the secret in the secrets database */
234
235         strupper(domain);
236
237         if (!secrets_store_domain_sid(domain, &domain_sid)) {
238                 DEBUG(0, ("error storing domain sid for %s\n", domain));
239                 goto done;
240         }
241
242         if (!secrets_store_machine_password(clear_trust_password)) {
243                 DEBUG(0, ("error storing plaintext domain secrets for %s\n", domain));
244         }
245
246         /* Now check the whole process from top-to-bottom */
247
248         cli_samr_close(cli, mem_ctx, &user_pol);
249
250         cli_nt_session_close(cli); /* Done with this pipe */
251
252         if (!cli_nt_session_open(cli, PIPE_NETLOGON)) {
253                 DEBUG(0, ("Error connecting to NETLOGON pipe\n"));
254                 goto done;
255         }
256
257         if (!secrets_fetch_trust_account_password(domain, 
258                                                   stored_md4_trust_password, NULL)) {
259                 DEBUG(0, ("Could not reterive secrets we just stored!"));
260                 goto done;
261         }
262         
263         CHECK_RPC_ERR(new_cli_nt_setup_creds(cli, stored_md4_trust_password),
264                           "error in domain join verification");
265         
266         retval = 0;             /* Success! */
267         
268 done:
269         /* Close down pipe - this will clean up open policy handles */
270
271         if (cli->nt_pipe_fnum)
272                 cli_nt_session_close(cli);
273
274         /* Display success or failure */
275
276         if (retval != 0) {
277                 trust_password_delete(domain);
278                 fprintf(stderr,"Unable to join domain %s.\n",domain);
279         } else {
280                 printf("Joined domain %s.\n",domain);
281         }
282         
283         cli_shutdown(cli);
284
285         SAFE_FREE(clear_trust_password);
286
287         return retval;
288 }
289
290