merge of new client side support the Win2k LSARPC UUID in rpcbind
[ira/wip.git] / source3 / utils / net_rpc_join.c
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
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.
11    
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.
16    
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.  */
20  
21 #include "includes.h"
22 #include "../utils/net.h"
23
24 /* Macro for checking RPC error codes to make things more readable */
25
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; \
30         }
31
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; \
36         }
37
38
39 /**
40  * confirm that a domain join is still valid
41  *
42  * @return A shell status integer (0 for success)
43  *
44  **/
45 int net_rpc_join_ok(const char *domain)
46 {
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;
53
54         /* Connect to remote machine */
55         if (!(cli = net_make_ipc_connection(NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC))) {
56                 return 1;
57         }
58
59         if (!cli_nt_session_open(cli, PI_NETLOGON)) {
60                 DEBUG(0,("Error connecting to NETLOGON pipe\n"));
61                 goto done;
62         }
63
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;
68         }
69         
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;
75         }
76
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");
81         
82         retval = 0;             /* Success! */
83         
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);
88
89         cli_shutdown(cli);
90
91         return retval;
92 }
93
94 /**
95  * Join a domain using the administrator username and password
96  *
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)
101  *
102  **/
103
104 int net_rpc_join_newstyle(int argc, const char **argv) 
105 {
106
107         extern pstring global_myname;
108
109         /* libsmb variables */
110
111         struct cli_state *cli;
112         TALLOC_CTX *mem_ctx;
113         uint32 acb_info;
114
115         /* rpc variables */
116
117         POLICY_HND lsa_pol, sam_pol, domain_pol, user_pol;
118         DOM_SID domain_sid;
119         uint32 user_rid;
120
121         /* Password stuff */
122
123         char *clear_trust_password = NULL;
124         fstring ucs2_trust_password;
125         int ucs2_pw_len;
126         uchar pwbuf[516], sess_key[16];
127         SAM_USERINFO_CTR ctr;
128         SAM_USER_INFO_24 p24;
129         SAM_USER_INFO_10 p10;
130
131         /* Misc */
132
133         NTSTATUS result;
134         int retval = 1;
135         fstring domain;
136         uint32 num_rids, *name_types, *user_rids;
137         uint32 flags = 0x3e8;
138         char *acct_name;
139         const char *const_acct_name;
140
141         /* Connect to remote machine */
142
143         if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) 
144                 return 1;
145
146         if (!(mem_ctx = talloc_init())) {
147                 DEBUG(0, ("Could not initialise talloc context\n"));
148                 goto done;
149         }
150
151         /* Fetch domain sid */
152
153         if (!cli_nt_session_open(cli, PI_LSARPC)) {
154                 DEBUG(0, ("Error connecting to SAM pipe\n"));
155                 goto done;
156         }
157
158
159         CHECK_RPC_ERR(cli_lsa_open_policy(cli, mem_ctx, True,
160                                           SEC_RIGHTS_MAXIMUM_ALLOWED,
161                                           &lsa_pol),
162                       "error opening lsa policy handle");
163
164         CHECK_RPC_ERR(cli_lsa_query_info_policy(cli, mem_ctx, &lsa_pol,
165                                                 5, domain, &domain_sid),
166                       "error querying info policy");
167
168         cli_lsa_close(cli, mem_ctx, &lsa_pol);
169
170         cli_nt_session_close(cli); /* Done with this pipe */
171
172         /* Create domain user */
173         if (!cli_nt_session_open(cli, PI_SAMR)) {
174                 DEBUG(0, ("Error connecting to SAM pipe\n"));
175                 goto done;
176         }
177
178         CHECK_RPC_ERR(cli_samr_connect(cli, mem_ctx, 
179                                        SEC_RIGHTS_MAXIMUM_ALLOWED,
180                                        &sam_pol),
181                       "could not connect to SAM database");
182
183         
184         CHECK_RPC_ERR(cli_samr_open_domain(cli, mem_ctx, &sam_pol,
185                                            SEC_RIGHTS_MAXIMUM_ALLOWED,
186                                            &domain_sid, &domain_pol),
187                       "could not open domain");
188
189         /* Create domain user */
190         acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname); 
191         strlower(acct_name);
192         const_acct_name = acct_name;
193
194         acb_info = ((lp_server_role() == ROLE_DOMAIN_BDC) || lp_server_role() == ROLE_DOMAIN_PDC) ? ACB_SVRTRUST : ACB_WSTRUST;
195
196         result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
197                                           acct_name, acb_info,
198                                           0xe005000b, &user_pol, 
199                                           &user_rid);
200
201         if (!NT_STATUS_IS_OK(result) && 
202             !NT_STATUS_EQUAL(result, NT_STATUS_USER_EXISTS)) {
203                 d_printf("Create of workstation account failed\n");
204
205                 /* If NT_STATUS_ACCESS_DENIED then we have a valid
206                    username/password combo but the user does not have
207                    administrator access. */
208
209                 if (NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED))
210                         d_printf("User specified does not have administrator privileges\n");
211
212                 goto done;
213         }
214
215         /* We *must* do this.... don't ask... */
216
217         if (NT_STATUS_IS_OK(result))
218                 cli_samr_close(cli, mem_ctx, &user_pol);
219
220         CHECK_RPC_ERR_DEBUG(cli_samr_lookup_names(cli, mem_ctx,
221                                                   &domain_pol, flags,
222                                                   1, &const_acct_name, 
223                                                   &num_rids,
224                                                   &user_rids, &name_types),
225                             ("error looking up rid for user %s: %s\n",
226                              acct_name, nt_errstr(result)));
227
228         if (name_types[0] != SID_NAME_USER) {
229                 DEBUG(0, ("%s is not a user account\n", acct_name));
230                 goto done;
231         }
232
233         user_rid = user_rids[0];
234                 
235         /* Open handle on user */
236
237         CHECK_RPC_ERR_DEBUG(
238                 cli_samr_open_user(cli, mem_ctx, &domain_pol,
239                                    SEC_RIGHTS_MAXIMUM_ALLOWED,
240                                    user_rid, &user_pol),
241                 ("could not re-open existing user %s: %s\n",
242                  acct_name, nt_errstr(result)));
243         
244         /* Create a random machine account password */
245
246         { 
247                 char *str;
248                 str = generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
249                 clear_trust_password = strdup(str);
250         }
251
252         ucs2_pw_len = push_ucs2(NULL, ucs2_trust_password, 
253                                 clear_trust_password, 
254                                 sizeof(ucs2_trust_password), 0);
255                   
256         encode_pw_buffer((char *)pwbuf, ucs2_trust_password,
257                          ucs2_pw_len);
258
259         /* Set password on machine account */
260
261         ZERO_STRUCT(ctr);
262         ZERO_STRUCT(p24);
263
264         init_sam_user_info24(&p24, (char *)pwbuf,24);
265
266         ctr.switch_value = 24;
267         ctr.info.id24 = &p24;
268
269         /* I don't think this is quite the right place for this
270            calculation.  It should be moved somewhere where the credentials
271            are calculated. )-: */
272
273         mdfour(sess_key, cli->pwd.smb_nt_pwd, 16);
274
275         CHECK_RPC_ERR(cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24, 
276                                             sess_key, &ctr),
277                       "error setting trust account password");
278
279         /* Why do we have to try to (re-)set the ACB to be the same as what
280            we passed in the samr_create_dom_user() call?  When a NT
281            workstation is joined to a domain by an administrator the
282            acb_info is set to 0x80.  For a normal user with "Add
283            workstations to the domain" rights the acb_info is 0x84.  I'm
284            not sure whether it is supposed to make a difference or not.  NT
285            seems to cope with either value so don't bomb out if the set
286            userinfo2 level 0x10 fails.  -tpot */
287
288         ZERO_STRUCT(ctr);
289         ctr.switch_value = 0x10;
290         ctr.info.id10 = &p10;
291
292         init_sam_user_info10(&p10, acb_info);
293
294         /* Ignoring the return value is necessary for joining a domain
295            as a normal user with "Add workstation to domain" privilege. */
296
297         result = cli_samr_set_userinfo2(cli, mem_ctx, &user_pol, 0x10, 
298                                         sess_key, &ctr);
299
300         /* Now store the secret in the secrets database */
301
302         strupper(domain);
303
304         if (!secrets_store_domain_sid(domain, &domain_sid)) {
305                 DEBUG(0, ("error storing domain sid for %s\n", domain));
306                 goto done;
307         }
308
309         if (!secrets_store_machine_password(clear_trust_password)) {
310                 DEBUG(0, ("error storing plaintext domain secrets for %s\n", domain));
311         }
312
313         /* Now check the whole process from top-to-bottom */
314         cli_samr_close(cli, mem_ctx, &user_pol);
315         cli_nt_session_close(cli); /* Done with this pipe */
316
317         retval = net_rpc_join_ok(domain);
318         
319 done:
320         /* Close down pipe - this will clean up open policy handles */
321
322         if (cli->nt_pipe_fnum)
323                 cli_nt_session_close(cli);
324
325         /* Display success or failure */
326
327         if (retval != 0) {
328                 trust_password_delete(domain);
329                 fprintf(stderr,"Unable to join domain %s.\n",domain);
330         } else {
331                 printf("Joined domain %s.\n",domain);
332         }
333         
334         cli_shutdown(cli);
335
336         SAFE_FREE(clear_trust_password);
337
338         return retval;
339 }
340
341
342 /**
343  * check that a join is OK
344  *
345  * @return A shell status integer (0 for success)
346  *
347  **/
348 int net_rpc_testjoin(int argc, const char **argv) 
349 {
350         char *domain = lp_workgroup();
351
352         domain = smb_xstrdup(domain);
353
354         /* Display success or failure */
355         if (net_rpc_join_ok(domain) != 0) {
356                 fprintf(stderr,"Join to domain '%s' is not valid\n",domain);
357                 free(domain);
358                 return -1;
359         }
360
361         printf("Join to '%s' is OK\n",domain);
362         free(domain);
363         return 0;
364 }